Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing fragments in an activity not calling onAttach, onCreate, onCreateView, etc

So I have this piece of code here, I am creating a new Fragment and replacing it with another fragment. That works well. However I have notice that on the first line the constructor is being called but the onAttach(), onCreate() etc are not. If I were to uncomment the second line it won't work as updateItem(URL) requires a webView that is initiated in the onCreate() function.

DetailViewFragment detailFragment = new DetailViewFragment();
//detailFragment.updateItem(URL);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.displayList, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Would appreciate any help that will get that to work with the second line uncommented.

like image 692
Castaldi Avatar asked Jul 25 '14 12:07

Castaldi


People also ask

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.

How do I attach a fragment to an activity?

Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.

Does fragment have onCreate?

onCreate(Bundle) called to do initial creation of the 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.

What is setRetainInstance true?

setRetainInstance(true); // Start up the worker thread. mThread.start(); } /** * This is called when the Fragment's Activity is ready to go, after * its content view has been installed; it is called both after * the initial fragment creation and after the fragment is re-attached * to a new activity.


1 Answers

The onAttach(), onCreate(), etc. will not be called until the FragmentManager has actually committed the change. So, some time after commit() is called on the transition. If you need to pass the URL to the Fragment from the start, add it to the fragment's argument bundle before you call commit(). Then you'll be able to get access to the URL in your onCreate() or other lifecycle methods. So you'll want something like this:

DetailViewFragment detailFragment = new DetailViewFragment();
Bundle args = new Bundle();
args.putString(DetailViewFragment.INIT_URL, URL);
detailFragment.setArguments(args);
ft.replace(R.id.displayList, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();

Now in your onCreate() you can call getArguments() to get the bundle and retrieve the URL which was passed by your activity.

like image 60
Larry Schiefer Avatar answered Oct 03 '22 23:10

Larry Schiefer