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 that savedInstanceState
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?
onCreateView(LayoutInflater, ViewGroup, Bundle) creates and returns the view hierarchy associated with the fragment. onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity. onCreate() .
These files contain only the onCreateView() method to inflate the UI of the fragment and returns the root of the fragment layout. If the fragment does not have any UI, it will return null.
onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.
The onActivityCreated() method is called after onCreateView() and before onViewStateRestored() . onDestroyView() : Called when the View previously created by onCreateView() has been detached from the Fragment . This call can occur if the host Activity has stopped, or the Activity has removed the Fragment .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With