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?
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With