Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: what is the difference between Fragment !isDetached() and isAdded()?

In my Fragment, I want to make a Toast once some data is loaded, like this:

Toast.makeText(getActivity(), R.string.toast_loading_done,
        Toast.LENGTH_SHORT).show();

This only makes sense if the Fragment is still attached to the Activity, i.e. the Fragment is visible and getActivity() will not return null.

I could check that with !isDetached() or with isAdded(), right? Here I am wondering what the difference between the two methods is and which one I should preferably use?

like image 206
Terry Avatar asked Jun 09 '15 10:06

Terry


1 Answers

A Fragment can be attached to an Activity without being added to it's view hierarchy.

If a Fragment is detached, it's state is maintained and can be re-added.

like image 185
Leandros Avatar answered Oct 21 '22 18:10

Leandros