frieza frieza - 1 year ago 132
Android Question

android fragment- How to save states of views in a fragment when another fragment is pushed on top of it

In android, a fragment (say

) gets added to the backstack and another fragment (say
) comes to the top. Now on hitting back
comes to the top and the
is called. Now I had
in a particular state before
got pushed on top of it.

My Question is how can I restore
to its previous state ? Is there a way to save state (like say in a Bundle) and if so then which method should I override ?

Answer Source

In fragment guide FragmentList example you can find:

    public void onSaveInstanceState(Bundle outState) {
        outState.putInt("curChoice", mCurCheckPosition);

Which you can use later like this:

    public void onActivityCreated(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
            // Restore last state for checked position.
            mCurCheckPosition = savedInstanceState.getInt("curChoice", 0);

I'm a beginner in Fragments but it seems like solution of your problem ;) OnActivityCreated is invoked after fragment returns from back stack.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download