Issue
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);
}
Solution
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 launchActivityB
(only if the amount of data is not too large)
Answered By - David Wasser
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.