Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments onCreateView() bundle. Where does it come from?

Tags:

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?

like image 223
Graeme Avatar asked Aug 12 '11 14:08

Graeme


People also ask

What is onCreateView fragment?

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() .

What does the onCreateView () method return if a fragment doesn't have any?

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.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

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.

What is called after onCreateView?

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 .


1 Answers

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.

like image 153
theisenp Avatar answered Jan 02 '23 15:01

theisenp