14

I am replacing a Fragment with another one:

    FragmentTransaction transaction = mFragmentManager.beginTransaction();

    transaction.setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit);
    transaction.replace(R.id.main_container, nextFragment, nextFragment.getClass().toString());
    transaction.addToBackStack(nextFragment.getClass().toString());
    transaction.commit();

however, the fragment thats being replaced, its onSaveInstanceState is not called. what am i doing wrong?

0xSina
  • 20,973
  • 34
  • 136
  • 253

2 Answers2

12

"In a Fragment, all of their lifecycle callbacks are directly tied to their parent Activity. So onSaveInstanceState gets called on the Fragment when its parent Activity has onSaveInstanceState called."

Look at this post:

FragmentActivity onSaveInstanceState not getting called

Community
  • 1
  • 1
kgmaize
  • 186
  • 1
  • 6
  • 2
    What are my options in this case then? – 0xSina Mar 28 '14 at 21:10
  • What were you trying to use onSaveInstanceState() for? – kgmaize Mar 28 '14 at 21:14
  • 3
    In my activity, I am presenting a step by step form. Each step is in a fragment. When the user presses next, I show the next fragment via the code above (in my question). However, when the user presses back, the previous step (fragment) appears but it's a new instance. That's why I am trying to save state, so that I can show user what he entered before he went on to the next step. – 0xSina Mar 28 '14 at 21:18
  • 4
    You should be able to just have a variable in your specific Fragment class that stores all of the data you need to persist when the user presses back and returns to the previous fragment. The Fragment instance doesn't actually get destroyed when going to the next step, though its views do. – kgmaize Mar 28 '14 at 21:26
  • 1
    @kgmaize, yes, really, in `onCreateView()` we can check `savedInstanceState` and `getArguments()` to get data from `Bundle`. And if we return from second fragment (`Activity` hasn't been destroyed), `onCreateView()` is called, `saveInstanceState == null` and all variables still exist. So, we can check, if our variable `isInitialized == false` and get new values. If `isInitialized == true` then skip setting new values. – CoolMind Nov 21 '18 at 16:54
0

As @StevenByle mentioned in https://stackoverflow.com/a/15935826/2519297

You can solve the problem by calling onSaveInstanceState(new Bundle()); directly inside of onPause

bebosh
  • 806
  • 10
  • 25
  • 1
    This is not a solution. He clearly says: "directly calling `onSaveInstanceState(new Bundle());` inside of `onPause`, **is a very bad practice**" – ar34z Dec 13 '22 at 14:44
  • If you go through link above in the @kgmaize answer, you will see the same what I wrote :D – bebosh Dec 20 '22 at 12:51
  • Steven Byle's answer (in that link) says: "The hack you added, directly calling onSaveInstanceState(new Bundle()); inside of onPause, is a very bad practice, as you should never call the lifecycle callbacks directly. Doing so can put your app into illegal states." – auspicious99 Jan 28 '23 at 11:45