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

4.4k Views Asked by At

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?

1

There are 1 best solutions below

1
On BEST ANSWER

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.