Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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?

like image 473
Peter Nelson Avatar asked Feb 07 '11 17:02

Peter Nelson


People also ask

What happens when finish is called Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

What happens when you call finish () inside onCreate ()?

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

What finish () function does?

When calling finish() on an activity, the method onDestroy() is executed. This method can do things like: Dismiss any dialogs the activity was managing. Close any cursors the activity was managing.

How do you check if activity is finished or not?

Using activity. isFinishing() is the right one solution. it return true if activity is finished so before creating dialog check for the condition. if true then create and show dialog.


2 Answers

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

like image 70
Nanne Avatar answered Oct 11 '22 14:10

Nanne


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.

like image 27
Robby Pond Avatar answered Oct 11 '22 16:10

Robby Pond