Calling finish() on an Android activity doesn't actually finish

Android

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

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

also, see this question: https://stackoverflow.com/questions/2590947/about-finish-in-android

Solution 2 - Android

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.

Solution 3 - Android

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

Solution 4 - Android

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

Solution 5 - Android

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.

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPeter NelsonView Question on Stackoverflow
Solution 1 - AndroidNanneView Answer on Stackoverflow
Solution 2 - AndroidRobby PondView Answer on Stackoverflow
Solution 3 - AndroidPedro LoureiroView Answer on Stackoverflow
Solution 4 - AndroidCodeToLifeView Answer on Stackoverflow
Solution 5 - AndroidFalmarriView Answer on Stackoverflow
Solution 6 - AndroidWJrView Answer on Stackoverflow