Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the fragment equivalent of Activity.isFinishing()?

Tags:

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 
like image 625
jph Avatar asked Aug 22 '14 15:08

jph


People also ask

What is isFinishing?

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.

How do I start an intent from a fragment?

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);

How do I recreate fragments?

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.

What is a fragment in Android Studio?

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.


2 Answers

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.

like image 179
Kevin Coppock Avatar answered Sep 21 '22 09:09

Kevin Coppock


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.

like image 44
jph Avatar answered Sep 22 '22 09:09

jph