I have Activity A. it displays some data retrieved from the server by calling void loadData() in onCreate.
User click a button which open a new Activity B for result. Once it finishes it back to Activty A. onActivtyresult should call loadData() to reload the data.
When Activty B finishes both onCreate and onActivityResult called as A was destroyed. Which is not Ok for me.
The question is:
Dose activity A always will be destroyed and onCreate will be called? Is this documented as I could not find any thing about this. If this is the case then I will call loadData only in onCreate.
If not I have to call loadData from onActivityResult also, which again leaded to call loadData 2 time in some cases, which I dont want.
Class A:
public void onCreate(@Nullable Bundle bundle) { .... creation code loadData(); // will be called first time activity created as well when back from the activity B } public void createNew() { Intent createService = new Intent(this,CreateService.class); startActivityForResult(createService, 1001); overridePendingTransition(R.anim.out_top,R.anim.fp); } public void onActivityResult(int i, int i2, Intent intent) { super.onActivityResult(i, i2, intent); if (i2 != -1) { loadData(); ///will be called when back from Activty B } }
Class B:
public void on taskFinish() { setResult(1); finish(); overridePendingTransition(R.anim.s_right, R.anim.s_in_right); }
I have come with a work around like this
boolean loaded = false;
void onCreate() {
loadData();
loaded=true;
}
public void createNew() {
loaded = false; /// Set before start the new Activity
Intent createService = new Intent(this,CreateService.class);
startActivityForResult(createService, 1001);
overridePendingTransition(R.anim.out_top,R.anim.fp);
}
public void onActivityResult(int i, int i2, Intent intent) {
super.onActivityResult(i, i2, intent);
if (i2 != -1) {
if (!loaded) {
loadData();
loaded = true;
}
}
}
I have 2 scansion
1- Activity created form scrach- loadData will be called once in OnCrate()
2- Start new Activity for result - set loaded = false
a- after back for result if activity destroyed then loaded == true in onCreate and loadData will be called once. then onActivityResult will be called but loadData will not be called.
b- after back for result if Activity not destroyed then loaded = false and loadData will be called
No, there is no guarantee that Activity A will be destroyed. You'd need to find some way of tracking whether the data needs to be reloaded - perhaps you could pass back a timestamp or something similar in the result from Activity B that you could use to check if the data you have in Activity A is up-to-date.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.