Reputation: 5977
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?
Upvotes: 53
Views: 58535
Reputation: 4171
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
Upvotes: 1
Reputation: 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();
}
}
Upvotes: -1
Reputation: 11514
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
Upvotes: 4
Reputation: 73494
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.
Upvotes: 7
Reputation: 64429
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
Upvotes: 79
Reputation: 48615
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
.
Upvotes: 0