简体   繁体   中英

Restoring state in Android when using the 'up' button

I am using onSaveInstanceState() to store an ArrayList member variable and restore it in the onCreate() method of the main activity. This works in most circumstances such as rotating the screen etc. but if I open a new activity and use the 'up' button (not the back button) to navigate back to the main screen it seems to create a new main activity without passing the state bundle in onCreate() .

I have confirmed that when the up button is pressed the onDestroy() method is called for the original instance of the main activity which makes no sense to me because I want it to resume the existing activity as if I had pressed the back button rather than create a new one.

Is there a way I can force the new activity to restore the old one or just resume the existing activity?

尝试在清单中将主要活动的启动模式设置为singleTop:

<activity android:name="activityName" android:launchMode="singleTop" ... />

have you tried to use:

 Intent i = new Intent(this, MainScreenActivity.class);
  i.setFlags(Intent.FLAG_CLEAR_TOP);
 startActivity(i);
  finish();

This code should called on pressing up button.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM