随着应用程序的过程中收集内存可能会在任何时候被破坏,这些静态实例很重要确实收集垃圾。
如果你把这些静态变量在自定义的应用程序对象,同样适用,除非你的每个应用程序获取(重新)创造了时间初始化它们在应用程序的onCreate功能。
你应该跟踪使用或者SharedPreferences或SQLite数据库持久性数据。
如果这些变量都太复杂,无法存储一样,那么你可能要考虑使用单(不推荐子类的应用,因为它曾经是)。
public class MySingleton {
public static MySingleton getInstance(Context context) {
if (instance==null) {
// Make sure you don't leak an activity by always using the application
// context for singletons
instance = new MySingleton(context.getApplicationContext());
}
return instance;
}
private static MySingleton instance = null;
private MySingleton(Context context) {
// init your stuff here...
}
private String id = null;
private String uniqueId= null;
private String token = null;
private String sessionId = null;
}
您已将null分配给您在此显示的所有变量。你在说哪个变量?当您使用会话类和公共静态成员 –
使用SharedPreferences保存您的变量,因为机器人可以在必要时清理内存。但我不觉得有必要将所有这些信息放在SharedPreferences中(以上仅为示例)。单身人士或应用程序类更好/最差的选择? – Houcine
@BheshGurung他们得到的 – shkschneider