Fragments onCreateView() bundle. Where does it come from?
To the best of my knowledge, onCreateView
and onCreate()
are both passed the bundle from onSaveInstanceState()
.
So if you override onSaveInstanceState()
and put data in the bundle, you should be able to retrieve it in onCreateView()
. That is why the documentation says that the bundle will be non-null when the fragment is re-constructed from a previous saved state.
Related videos on Youtube
Comments
-
Graeme almost 2 years
I'm starting an Activity through the usual means:
Intent startIntent = new Intent(this, DualPaneActivity.class); startIntent.putExtras(((SearchPageFragment) currentFragment).getPageState()); startActivity(startIntent);
When this activity loads, it places a Fragment in a frame like so:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); Fragment currentFragment = fragment; currentFragment.setArguments(getIntent().getExtras()); transaction.replace(R.id.singlePane, currentFragment); transaction.commit();
Seems simple, right?
However, you can inside of onCreateView() method access three separate bundles (four, if you include the one included in the Fragment's
onCreate(Bundle savedInstanceState)
):@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Fill state information Bundle bundle; if(savedInstanceState != null) bundle = savedInstanceState; // 1 else if(getArguments() != null) bundle = getArguments(); // 2 else bundle = getActivity().getIntent().getExtras(); // 3 setPageState(bundle); }
In the above instance, I've worked out from trial and error that the bundle I want is the second one, the one retrieved from
getArguments()
.From my understanding the third one from
getActivity().getIntent().getExtras();
is actually calling the Bundle from the intent used to start containing activity. I also know from experimentation thatsavedInstanceState
seems to always be null. But where is it coming from and why is it null?The documentation says this:
savedInstanceState If non-null, this fragment is being re-constructed from a previous saved state as given here.
That doesn't help me - It's bugging me more than stopping me from moving on. Can someone help me out with this annoyance?