52

I'm calling finish() but my activity keeps on going.

I have an activity which is invoked by a menu from the main activity screen. In my activity's onCreate() method I have the following code fragment:

    // Make sure there are some events in the list.
    if (theEventArrayList.isEmpty()){
        Toast.makeText(this, "Event List is empty", Toast.LENGTH_LONG).show();
        finish();
    }
    SummarizeCurrentEvent();
    graphEvents();

If the list is empty it puts up the Toast, and I can set breakpoint on the call to finish(). If I step from that in the debugger it goes to straight to SummarizeCurrentEvent(). I thought finish() would exit the activity. Is this not the case? Where can I find out more information about this method?

Chris Smith
  • 18,244
  • 13
  • 59
  • 81
Peter Nelson
  • 5,917
  • 17
  • 43
  • 62

6 Answers6

79

You should put a return statement after that finish, because the method that called finish will be executed completely otherwise.

also, see this question: about finish() in android

Community
  • 1
  • 1
Nanne
  • 64,065
  • 16
  • 119
  • 163
  • 1
    Wouldn't it be better to put `SummarizeCurrentEvent(); graphEvents();...` in an `else {}` block? – ban-geoengineering Sep 19 '14 at 09:40
  • @ban-geoengineering You could potentially have multiple ``if (...) { finish(); return; }``. I think it's a good habit to explicitly ``finish()`` **and** ``return`` when there's code following. – Stephan Henningsen Feb 22 '16 at 08:09
7

finish() just tells the activity to do what it needs to do to finish, eg. shutdown, call on onPause, report result to parent, etc. It doesn't do an exit() call or anything.

You should return after the finish() call.

Robby Pond
  • 73,164
  • 16
  • 126
  • 119
4

Adding to the other answers, you still may have (Re)onStart, onResume and onPause invoked.

I say this because in the following link, there is a table that says that for one activity to be killed, first it is invoked onPause (and probably but not guaranteed) on Stop and onDestroy.

Reference Activity

RevanthKrishnaKumar V.
  • 1,855
  • 1
  • 21
  • 34
Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
1

put in manifest :

    <activity android:name=".MainActivity"
        android:noHistory="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

to avoid holding it in history stack of the system

CodeToLife
  • 3,672
  • 2
  • 41
  • 29
0

Finish finishes the activity, but it's up to the main loop to do any UI interaction. You have to wait until the UI loop runs, which is after you return from onCreate.

Falmarri
  • 47,727
  • 41
  • 151
  • 191
-1

This is the case where the try...catch statement should be used.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        //...some initialization...

        // Make sure there are some events in the list.
        if (theEventArrayList.isEmpty()){
            throw new Exception("Event List is empty");
        }
        SummarizeCurrentEvent();
        graphEvents();
    } catch (Exception e) {
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        finish();
    }
}
WJr
  • 1
  • 2