Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android prevent restoring the activity

Tags:

android

I have a viewpager holding many fragments. When the activity is sent to background and when resources are needed, the OS will kill the app or at least some fragments. When I return to the activity it crashes because the activity tries to attach new instances of all fragments it held before the clean-up and now some fields are null. This of course could be fixed by properly implementing state saving and restoring using Bundles, but I don't want to do that. Instead I want to prevent restoring the fragments. Is there a way to tell the OS that once it has sent the GC in and destroyed fragments, it shouldn't bother recreating them at all? Once the cleaning-up happens, I want the activity to be simply recreated on return, as if a user launched it by taping the icon. Any chance of doing that?

The proposed solution here https://stackoverflow.com/a/15683289/552735 does not work. It causes exceptions java.lang.IllegalStateException: Fragement no longer exists for key f2: index 3

like image 253
delegate Avatar asked Aug 06 '13 22:08

delegate


People also ask

What happens when an activity is recreated?

So, if your activity instance is destroyed and recreated, the state of the layout is restored to its previous state with no code required by you. However, your activity might have more state information that you'd like to restore, such as member variables that track the user's progress in the activity.

What is the use of savedInstanceState in Android?

The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle.

What is taskaffinity in Android?

Task affinity lets you define which task an activity belongs to. By default, an activity has the same task affinity as its root activity. With task affinity, we can now separate activities into different tasks.

How do you restore the state of an Android activity?

State can be restored in either the onCreate() or the onRestoreInstanceState() methods of the activity by extracting values from the Bundle object and updating the activity based on the stored values.


2 Answers

I had a problem just like this. Here's how I fixed it:

    @Override 
    protected void onSaveInstanceState(Bundle savedInstanceState)
    {
        savedInstanceState.clear();
    }

Note that this method will ensure that absolutely no UI state information will be stored when the Activity is killed by the system - this has the effect of also not restoring any Views when onRestoreInstanceState() gets called with the same Bundle after onStart().

like image 167
dadude999 Avatar answered Oct 31 '22 18:10

dadude999


You can't disable the save/restore instance state actions. In case you don't want to actually implement this logic, you have to reload the form especially if your form heavily loaded with fragments.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
     startActivity(new Intent(this,YOUR_ACTIVITY.class));
     finish();
}
like image 28
Yan Avatar answered Oct 31 '22 19:10

Yan