In my activities I frequently use this idiom:
@Override
public void onDestroy() {
super.onDestroy();
if (isFinishing() != true) return;
// do some final cleanup since we're going away for good
}
Fragment has an onDestroy()
method, but what is the equivalent of isFinishing()
? Should I just check getActivity().isFinishing()
from within the fragment's onDestroy()
?
EDITED TO ADD:
Here are the callbacks (in order) I get under various circumstances, along with whether getActivity()
returns null or non-null and, if non-null, the value of getActivity().isFinishing()
:
Scenario #1: DialogFragment
is showing and user taps back button (i.e. need to release references):
onDismiss(): activity = non-null, finishing = false
onDestroy(): activity = non-null, finishing = false
Scenario #2: DialogFragment
is showing and parent activity finishes (i.e. need to release references):
onDestroy(): activity = non-null, finishing = true
onDismiss(): activity = null, finishing = n/a
Scenario #3: DialogFragment
is showing and OS temporarily destroys parent activity (i.e. should not release references):
onDestroy(): activity = non-null, finishing = false
onDismiss(): activity = null, finishing = n/a
Fragments have a method called isRemoving()
which is true when the fragment is being removed from the Activity:
Return true if this fragment is currently being removed from its activity. This is not whether its activity is finishing, but rather whether it is in the process of being removed from its activity.