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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s