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
Android only kills processes. Unless you do something special, all of your activities are running in the same process, so can only all be killed together. Regardless, isFinishing() tells you if the activity is actually finishing. That is, the user can never return to it.
If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent);
attach() for recreating the fragment. Re-attach a fragment after it had previously been deatched from the UI with detach(Fragment). This causes its view hierarchy to be re-created, attached to the UI, and displayed. Detach the given fragment from the UI.
A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.
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.
Thanks for the help guys. I ended up adding these to my DialogFragment:
private void doReleaseReferences() { // release the stuff } @Override public void onDestroy() { super.onDestroy(); if (getActivity().isFinishing()) doReleaseReferences(); } @Override public void onDismiss(DialogInterface dialog) { super.onDismiss(dialog); if (getActivity() != null) doReleaseReferences(); }
Based on the callback behavior I added to the original question I think this serves my purposes. When the back button is pressed or the parent activity finishes the references are released; when the parent activity is destroyed by the OS with the intention of being revived later they are not released.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With