简体   繁体   中英

Force recreation of Activity/Fragment on restore

I am having a number of issues around saving app state to a Bundle via onSaveInstanceState().

For now, I need to address the issue of my app crashing when it restored after being closed by Android.

When an Activity/Fragment (in background) is being force closed by Android to regain resources, is there a way I can say "don't try and restore state when resume, just create everything anew"?

Thanks

In the "onCreate" method f the activity, you can pass a null value to the super call

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
    //rest of your code
}

I recommend to you save your data in onPause() method. Whe you close app with HOME button onSaveInstanceState never have been called and data will be lost.

is there a way I can say "don't try and restore state when resume, just create everything anew"?

Yes. You can. You can check window state of app and manipulate your logic with boolean flags. Also i will recommend to you slice save\\restore data process into 2 separate ways :

  • Prepare Data to save \\ restore
  • Save \\ Restore

For activity if you want to create it from scratch then just override onSaveInstanceState and call super.onSaveinstanceState passing a null al the Bundle.

For Fragment just set setRetainInstanceState to false in onActivityCreated method of the Fragment.

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