OnDestroy() 在启动 Activity 后在 onCreate() 之后被调用。

huangapple go评论83阅读模式
英文:

OnDestroy() is being called after onCreate() when I start Activity

问题

我在活动A的onDestroy()中更新本地数据库。活动B依赖于在活动A的onDestroy()中更新的本地数据库。

我的问题是,每当我启动活动B并完成活动A后,活动A的onDestroy()在活动B的onCreate()之后被调用。由于这个问题,我正在失去在销毁活动A后存储的数据。

我该如何解决这个问题?

活动A:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // 在检索用户数据后
    // 在点击监听器内
    someButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            finish();
            startActivity(this, FamilyInfoActivity.class);
        }
    });
}

@Override
protected void onDestroy() {
    localJson.setStatus(status);
    localDBUtil.setLocalJson(this, localJson, connectionId);
    super.onDestroy();
}

活动B:

@Override
protected void onCreate(Bundle savedInstanceState) {
    localJson = localDBUtil.getLocalJson(this, connectionId);
}
英文:

I am updating the local database in onDestroy() of activity A. Activity B is depends on the local DB which is updated in onDestroy() of activity A.

My problem is that whenever I start Activity B and finish Activity A onDestroy() of activity A is being called after onCreate() of Activity B. Due to this issue I am losing the data stored after destroying Activity A.

How do I fix this issue?

Activity A

@Override
protected void onCreate(Bundle savedInstanceState){

    //After retrieving User data
    //inside onclicklistener
    someButton.setOnClickLister( new View.OnClickListener() {
        public void onClick(View view) {
              finish();
	          startActivity(this,FamilyInfoActivity.class);
	    }
    }
}

@Override
protected void onDestroy(){
	localJson.setStatus(status);
	localDBUtil.setLocalJson(this,localJson,connectionId);
	super.OnDestroy();
}

Activity B

 @Override
protected void onCreate(Bundle savedInstanceState){

	localJson = localDBUtil.getLocalJson(this,connectionId);
	
}

答案1

得分: 3

你不能依赖于 onDestroy() 的时机来保存您的更改。您应该在 onPause() 中保存更改,这是唯一保证会被调用的生命周期方法。

另外,如果您想要从 ActivityA 传递数据到 ActivityB,您可以使用以下方法之一:

  • 将数据存储在文件中
  • 将数据存储在SQLite数据库中
  • 将数据存储在 SharedPreferences
  • 将数据放入启动 ActivityBIntent 的 "extras" 中(只有数据量不太大时才适用)
英文:

You cannot rely on the timing of onDestroy() to save your changes. You should save changes in onPause() which is the only lifecycle method that is guaranteed to be called.

Also, if you want to pass data from ActivityA to ActivityB, you can use one of the following methods:

  • Store data in a file
  • Store data in an SQLite database
  • Store data in SharedPreferences
  • Put the data in "extras" in the Intent you use to launch ActivityB (only if the amount of data is not too large)

答案2

得分: -1

请在onCreate中删除finish()finish会销毁您的Activity,这就是为什么会调用onDestroy的原因。

英文:

Please remove finish() in onCreate. Finish is destroying your Activity that's why onDestroy is being called

huangapple
  • 本文由 发表于 2020年7月28日 16:45:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63130275.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定