Tag Archives: IllegalStateException

Fragment isAdded() vs getActivity()

In Android fragments, its a common practice to check if the fragment is still attached to the activity before performing some action, to avoid IllegalStateException. There seem to be several similar ways to achieve this. Checking if getActivity() == null is one way. However a more reliable way seems to be to use isAdded(). This internally checks if the activity is not null, as well as checks if the current fragment is added to the activity.