Java 王国赢得';关闭应用程序后,不要保存变量/对象。我该如何解决这个问题?

Java 王国赢得';关闭应用程序后,不要保存变量/对象。我该如何解决这个问题?,java,android,database,timer,realm,Java,Android,Database,Timer,Realm,我已经有一个领域对象可以很好地处理这个领域。但是,我的Points对象没有保存。在Android emulator上运行应用程序时,将其从recents中清除会清除points类的points变量。整个应用程序中只有一个点实例。我将在下面插入如何使用要点的代码。我还观察到使用Stetho和chrome在领域中更新变量,因此它肯定会保存到数据库中 我最初在领域中创建对象的MyApplication类: public class MyApplication extends Application {

我已经有一个领域对象可以很好地处理这个领域。但是,我的Points对象没有保存。在Android emulator上运行应用程序时,将其从recents中清除会清除points类的points变量。整个应用程序中只有一个点实例。我将在下面插入如何使用要点的代码。我还观察到使用Stetho和chrome在领域中更新变量,因此它肯定会保存到数据库中

我最初在领域中创建对象的MyApplication类:

public class MyApplication extends Application {

static Realm realm;

@Override
public void onCreate() {
    super.onCreate();

    Realm.init(this);

    RealmConfiguration config = new RealmConfiguration.Builder()
            .name("chargerpoints.realm")
            .schemaVersion(1)
            .build();

    realm = Realm.getInstance(config);
    realm = Realm.getDefaultInstance();

    Points pts = realm.where(Points.class).findFirst();

    if(pts == null) {
        Points myPoints = new Points();
        myPoints.setId(1);
        myPoints.setPts(0);
        realm.beginTransaction();
        realm.copyToRealmOrUpdate(myPoints);
        realm.commitTransaction();
    }
MainActivity,其中我使用计时器递增myPoints对象的points整数变量:

static int points;

Points myPoints;

Context context = this;

Calendar c = Calendar.getInstance();
int hours = c.get(Calendar.HOUR_OF_DAY);

private Timer timer;
private TimerTask timerTask = new TimerTask() {
    @Override
    public void run() {
        if(hours > 7 && hours < 15){
            KeyguardManager myKM = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
            if (myKM.inKeyguardRestrictedInputMode()) {
                points++;
            } else {
                realm = Realm.getDefaultInstance();
                myPoints = realm.where(Points.class).findFirst();
                realm.beginTransaction();
                myPoints.setPts(points);
                realm.copyToRealmOrUpdate(myPoints);
                realm.commitTransaction();
            }
            displayPoints();
        }
    }
};
静态int点;
点myPoints;
上下文=这个;
Calendar c=Calendar.getInstance();
int hours=c.get(日历小时);
私人定时器;
专用TimerTask TimerTask=新TimerTask(){
@凌驾
公开募捐{
如果(小时数>7和小时数<15){
KeyguardManager myKM=(KeyguardManager)context.getSystemService(context.KEYGUARD_SERVICE);
if(myKM.inKeyguardRestrictedInputMode()){
积分++;
}否则{
realm=realm.getDefaultInstance();
myPoints=realm.where(Points.class).findFirst();
realm.beginTransaction();
myPoints.设定值(点);
realm.copyToRealmOrUpdate(myPoints);
realm.commitTransaction();
}
显示点();
}
}
};

我基本上已经尝试了所有可能的组合,到目前为止还没有接近Points类的数据持久性。我已经有另一个对象可以很好地处理Realm,所以这一个为什么不能工作,这让人很困惑。谢谢你的帮助

1)不要将领域变量保存在静态变量中,您可以根据需要打开/关闭实例。2) 不要使用beginTransaction-code-commitTransaction(您的代码中缺少commitTransaction)结构,而是使用
executeTransactionAsync
3)首先使用配置查找域,然后立即将领域实例设置为
getDefaultInstance
——也许您正在寻找
setDefaultConfiguration
?我在这两段代码中都有
commitTransaction
,但我将试用
executeTransactionAsync
。此外,我还将试用
setDefaultConfiguration
。我会让你知道它是否有效@nbokmans谢谢@不幸的是,这些改变并不能解决我的积分不保存的问题。您知道为什么会发生这种情况吗?在计时器任务中调用
realm.close()
。在不关闭领域实例的情况下,如果该线程没有活套,则该线程上的领域可能永远保留在旧版本的数据中。@Beender我尝试过,但在从最近的应用中清除该应用后,它仍然无法保存。