4

Checking some legacy code I've found this snippet:

@Override
public void onResume() {
    if (!isFinishing()) {
        ...
    }
    super.onResume();
}

despite the super.onResume() call at the end of the method, which is discouraged:

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work, as shown in the examples above http://developer.android.com/guide/components/activities.html

I'm concerned about the if (!isFinishing()) call, does this have sense? as I can see checking Activity code mFinished variable is set to true only on finish() and finishActivity(), can, through the Android Lifecycle, to be resumed an activity which is being destroyed?

Thanks in advance.

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
  • That is interesting. Where did you found that code? – shkschneider Apr 24 '15 at 13:52
  • Is the main activity of my company aplication. – Guillermo Merino Apr 24 '15 at 13:55
  • onDestroy(): The final call you receive before your activity is destroyed. This can happen either because the activity is finishing (someone called finish() on it, or because the system is temporarily destroying this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing() method. https://developer.android.com/reference/android/app/Activity.html – shkschneider Apr 24 '15 at 13:55
  • @shkschneider I've seen that, but does this have sense in onResume()? I'm assuming that finish() or the system internal call is being executed on UI thread (http://stackoverflow.com/questions/7036151/can-i-kill-or-finish-an-activity-in-a-thread-other-than-ui-thread) so, will the activity be resumed while is being destroyed? – Guillermo Merino Apr 24 '15 at 14:00
  • I agree that this code in `onResume()` and even before `super.onResume()` makes no sense to me whatsoever. – shkschneider Apr 24 '15 at 14:03

2 Answers2

2

Finally, the legacy code was calling finish() under some circunstances at the onCreate() method. But taking a look at onCreate() javadoc:

You can call finish() from within this function, in which case onDestroy() will be immediately called without any of the rest of the activity lifecycle (onStart(), onResume(), onPause(), etc) executing.

So, this isFinishing() call is useless inside onResume()

Guillermo Merino
  • 3,197
  • 2
  • 17
  • 34
1

The answer to you question is "no" activity cannot be resumed if it was destroyed.
Here is good discussion: Understanding of isFinishing()

The reason for this code may be to distinguish between orientation change and actual finishing of the activity Important to note here is isFinishing: true which means that call to isFinishing() in the onDestroy() returns true, i.e. which happens when:

User hits "back" button OR activity's code calls it's finish() (isFinishing() returns false when activity is geting closed after phone rotaion in order to be started again)

http://ogrelab.ikratko.com/activity-lifecycle-explained-in-details/

Community
  • 1
  • 1
Radoslav
  • 1,446
  • 1
  • 16
  • 30