2

I've read the documentation of Android Activity Lifecycles, and something that it seems to be unclear on is:

What is the convention on calling finish()?

In particular:

  1. I create an Intent for another app. Do I call finish() on my current activity?
  2. I trigger a login page, LoginActivity, called by MainActivity. Do I call finish() on MainActivity when starting LoginActivity? Do I call finish() on LoginActivity when I finish authentication?
Max Feng
  • 352
  • 3
  • 10
  • I don't think it's a duplicate - I know what `finish()` does, I'm asking about the convention on when to call it, because it doesn't seem to be explicitly listed anywhere. – Max Feng Jun 24 '16 at 00:13
  • @MaxFeng - see my answer – ceph3us Jun 24 '16 at 09:00

3 Answers3

1

There is no single correct answer to your question. It depends a lot on what the workflow in your app is.

When ActivityA launches ActivityB and does not call finish(), then ActivityA is still present in the Activity stack in the task. When ActivityB ends, ActivityA will be shown as it is now the top Activity in the stack.

When ActivityA launches ActivityB and calls finish() on itself, then ActivityA is no longer present in the Activity stack in the task. When ActivityB ends, ActivityA will not be shown as it is no longer in the stack. The Activity underneath ActivityA will be shown (if there is one), otherwise the task ends as there are no longer any live activities in it.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
0

if we assume that:

 Activity is "Window Manager"  

-- as its main component is Window )

so calling finish() is equal to Close button in Window'ed application :)

  • is used to reclaim resources.

to confirm this assumption we can look at Activity class definition

public class Activity extends ContextThemeWrapper
    implements LayoutInflater.Factory2,
    Window.Callback, KeyEvent.Callback,
    OnCreateContextMenuListener, ComponentCallbacks2,
    Window.OnWindowDismissedCallback {

  ...

   private WindowManager mWindowManager;
   /*package*/ View mDecor = null;

  ...


    /**
     * Call this when your activity is done and should be closed.  The
     * ActivityResult is propagated back to whoever launched you via
     * onActivityResult().
     */
    public void finish() {
        finish(false);
    }

    /**
     * Finishes current activity & specifies whether to remove the task associated with it.
     */
    private void finish(boolean finishTask) { 
        ...
    }
}

to answer to your question You need know what exactly you are planning to do with your activity later ??? - generally predict its lifetime !

  • leave it on stack for later use
  • release only resources and hold instance (to omit time consuming later initialization)
  • destroy its instance (in case you will not use it anymore) - as finish

when to call finish??? depends on many factors that are present in the application code and it's personalized and requires an individual approach.

ceph3us
  • 7,326
  • 3
  • 36
  • 43
-1

finish() is mostly called when your activity is created to get a result (startActivityForResult).

It receives datas, calculate something and then calls finish to get back to the main app.

It is also implicitly called when pressing the back button.

Finally, it is used in a master / details pair of activities. For example, you select an article to read it and you come back to the list after it. But you should use fragments in order to achieve this.

Alexandre Martin
  • 1,472
  • 5
  • 14
  • 27
  • So in my example, I would not need to call `finish()` on `MainActivity`, but I should on `LoginActivity`? – Max Feng Jun 24 '16 at 00:12
  • 2
    If your main entry point is the MainActivity (Main Launcher in the Manifest), you could call LoginActivity if the user is not already connected and then call finish to get back to the main activity. This way, your activity pile will contains Main Activity instead of MainActivity / LoginActivity/ MainActivity which is more efficient – Alexandre Martin Jun 24 '16 at 00:15
  • This isn't true. `finish()` is called to end an `Activity` (remove it from the `Activity` stack in the task, trigger the call to `onDestroy()` so that the GC can reclaim resources). That's it. It makes no difference if the `Activity` has been started with `startActivity()` or `startActivityForResult()`. If `finish()` is not called (either explicitly by you or implicitly by the Android framework) then your `Activity` is still alive, active and present in the `Activity` stack in the task. – David Wasser Jun 24 '16 at 08:30