8

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

Community
  • 1
  • 1
delegate
  • 121
  • 3
  • 8
  • 1
    Have you tried to add `finish()` in `onStop()` method of the activity that is holding your fragents? It will not save states of anything. – Nikola Despotoski Aug 06 '13 at 22:59
  • That kills the activity every time it's sent to background, even when I press home button. This is not what I want - I only want that the OS stops recreating all fragments that existed before it has destroyed them. As far as I can see the ViewPager has it's own state restoring mechanism, that's what causing these problems. – delegate Aug 07 '13 at 07:40

2 Answers2

15

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().

dadude999
  • 306
  • 2
  • 8
  • 3
    This is not a solution this is symptom treatment – Roel Oct 29 '14 at 14:04
  • 3
    This is a very good solution if restoring activity state is not crucial or important at all. This way, the whole activity gets recreated again and everything works fine. – Nicolás Arias Mar 04 '17 at 17:45
1

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();
}
Yan
  • 1,569
  • 18
  • 22
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Badacadabra Jun 04 '17 at 12:16