简体   繁体   中英

How to kill all open fragments after onSaveInstanceState() of activity

I am using one activity multiple fragments architecture in my app. I know that when system kill the app in background and if then the app is brought to foreground, then the activity and all the opened fragments gets recreated. When the fragments are getting recreated, I am getting null pointer exceptions and null values calls to api at places.

Now I want to just kill all the fragments whenever memory of activity is cleared. And restart whole app flow from splash screens. If I try to remove fragments and commit then I am not able to do that because its asynchronous and can't be done after onSaveInstanceState(). If I try to do conmitAllowingStateLoss then its not clearing all the fragments its just stopping fragments to get recreated.

How do I clear all the fragments on onSaveInstanceState() call?

There are few different ways to do this, depending on your exact needs. The easiest way is to add this to the <activity> declaration for the root Activity of your app (the one with ACTION=MAIN and CATEGORY=LAUNCHER):

android:clearTaskOnLaunch="true"

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