Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SavedInstanceState is always null in fragment

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)); } 
like image 335
TommyNecessary Avatar asked Dec 12 '13 17:12

TommyNecessary


People also ask

Why is savedInstanceState null?

savedInstanceState will always remain null after the app is killed. It is not passed on to the bundle. public void onCreate(Bundle savedInstanceState) { super. onCreate(savedInstanceState); if (savedInstanceState !=

What does savedInstanceState mean?

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 the purpose of using onSaveInstanceState () here?

Basically, onSaveInstanceState is used for the scenario where Android kills off your activity to reclaim memory. In that scenario, the OS will keep a record of your activity's presence, in case the user returns to it, and it will then pass the Bundle from onSaveInstanceState to your onCreate method.

What is the use of protected void onCreate bundle savedInstanceState?

After Orientation changed then onCreate(Bundle savedInstanceState) will call and recreate the activity and load all data from savedInstanceState. Basically Bundle class is used to stored the data of activity whenever above condition occur in app.


2 Answers

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.

like image 140
TommyNecessary Avatar answered Sep 28 '22 18:09

TommyNecessary


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.

like image 38
Arun Yogannadan Avatar answered Sep 28 '22 20:09

Arun Yogannadan