Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment methods: attach(), detach(), remove(), replace(), popBackStack()

I am very confused between these functions and their purposes. What I have observed that using replace() replaces the existing fragment with a new one. We can use addToBackStack(null) to put that fragment in back stack so we can go back to the previously shown fragment. Now when a fragment is added (or replaced) - onAttach() -> onCreate() etc.... methods of the fragment are called in order.

Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order?

What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which functions of the fragment are called and in which order??

Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??

And when does onDestroy() called??

Thank you.

like image 659
Vikram Gupta Avatar asked Aug 28 '12 06:08

Vikram Gupta


People also ask

Does popBackStack destroy fragment?

FragmentManager popBackStack doesn't remove fragment.

What is the fragment method?

Android Fragment is the part of activity, it is also known as sub-activity. There can be more than one fragment in an activity. Fragments represent multiple screen inside one activity. Android fragment lifecycle is affected by activity lifecycle because fragments are included in activity.

What is the use of popBackStack in Android?

popBackStack. Pop all back stack states up to the one with the given identifier. This function is asynchronous -- it enqueues the request to pop, but the action will not be performed until the application returns to its event loop.

How do you detach a fragment?

To detach an added Fragment from an Activity, you use: getFragmentManager(). beginTransaction(). detach(mFragment). commit().


2 Answers

Now when we call remove() on the fragment from our activity, which functions of the fragment are called and in which order?

Look at http://developer.android.com/reference/android/app/Fragment.html .

The order is: onPause(), onStop(), onDestroyView(), onDestroy(), onDetach()

What does attach() and detach() do? Does detach() remove the fragment? And when these two attach() and detach() are used, which functions of the fragment are called and in which order??

attach() and detach() is respectively associates or detaches the Fragment with/from the Activity. When attaching the Fragment, the onAttach() lifecycle method is called, when detaching, the onDetach() lifecycle method is called in the Fragment. For more information look at the link above.

Also, what happens on popBackStack()?? I mean which functions are called when we use popBackStack()on the fragment from our activity??

If the Fragment hasn't been destroyed, then on popBackStack() the onStart() and onResume() methods are called. If the Fragment has been destroyed previously, then the lifecycle methods will be called starting from onAttach(). It's the same as, when you press the back button on Activities.

like image 130
Adam Monos Avatar answered Sep 22 '22 13:09

Adam Monos


Just a note on popBackStack(). It doesn't pop a fragment, it pops a fragment transaction. So whatever the last fragment transaction was is reversed. If you were displaying a FragmentA and your transaction was:

fragmentTransaction.replace(R.id.your_layout, fragmentB); fragmentTransaction.addToBackStack(null); 

It would replace FragmentA with FragmentB, and add that transaction (not the fragment) to the back stack. If you then hit the back button, it pops the back stack and gets the transaction, which was "replace this FragmentA with a FragmentB". It then reverses that transaction. Backwards, the instruction is to replace whatever the current fragment is with FragmentA. If the original FragmentA still exists, it uses that one. If it's been destroyed, it makes a new one.

like image 33
emote_control Avatar answered Sep 20 '22 13:09

emote_control