SavedInstanceState is always null in fragment

2019-01-10 23:03发布

I have a fragment attached to the activity using XML (and setContentView() in activity). A have a problem because I have very dynamic views in my fragment, so when orientation changes I must restore all states of views.

I have problem because I'm using something like that:

public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBoolean("restore", true);
        outState.putInt("nAndroids", 2);
   }

But after orientation change when methods with param Bundle savedInstanceState are called (like onCreateView etc) my savedInstanceState is always null.

I'm not a noob in the Android but now I'm very angry because of this problem...

public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    if (savedInstanceState == null) {
        //smth
    } else {
        // smthelse THIS IS NEVER REACHED BECAUSE BUNDLE IS ALWAYS NULL
    }

    getListView().setDivider(getResources().getDrawable(R.drawable.list_divider));
}

5条回答
贪生不怕死
2楼-- · 2019-01-10 23:17

Ok I know this is an old post but I couldn't find the right answer for me here nor many other places, so I am posting how I fixed my case.

So My Fragment is inside an Activity. And I originally tried to save Bundle only in Fragment and retrieve it at onCreateView. However that was the problem.

I fixed this by initiating myFragment object in activity and put that object to activity Bundle at onSaveInstanceState(). Then retrieved it at onRestoreInstanceState(). I used getSupportFragmentManager().putFragment/getFragment. Then the savedInstanceState in fragment was no longer null.

查看更多
放荡不羁爱自由
3楼-- · 2019-01-10 23:20

I had a similar problem where I was always getting savedInstanceState as null inspite of supplying the bundle to the Fragment.

The only solution that worked for me was to do

myFragment.setArguments(bundle) 

with my bundle from the Activity and do a

Bundle bundle = this.getArguments();

in onCreateView of the fragment.

Hope this helps someone else.

查看更多
看我几分像从前
4楼-- · 2019-01-10 23:24

First you should put your data, then call super.onSaveInstanceState(outState);

public void onSaveInstanceState(Bundle outState) {

    outState.putBoolean("restore", true);
    outState.putInt("nAndroids", 2);
    super.onSaveInstanceState(outState);
}

And be sure that activity has not nohistory property in AndroidManifest.xml or set it to false.

        <activity
        android:noHistory="false">
查看更多
Luminary・发光体
5楼-- · 2019-01-10 23:31

For Fragment :-

use this for save state of fragment on orientation.

onCreate(Bundle save)
{
   super.onCreate(save);
   setRetainInstance(true);
}

See this tutorial :- http://techbandhu.wordpress.com/2013/07/02/android-headless-fragment/

For Activity:-

When you start your application, in onCreate, your bundle object is null, so you have to put a check like below and when you rotate your screen then onSaveInstance is called and your bundle object is initialized

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);

    if (savedInstanceState != null) {
         boolean t = outState.getBoolean("restore"); 
         int s = outState.getInt("nAndroids");
    }
}
查看更多
Ridiculous、
6楼-- · 2019-01-10 23:38

All the problem was in that I don't declare android:id for the fragment in XML. Android needs ID or TAG to recognize stored fragment and reproduce all elements in it. So guys, remember - every instance of fragment needs unique id or tag!

Also, when setRetainInstance(true) is declared then bundle should always return null.

查看更多
登录 后发表回答