First two consideration:
- Don't use an exit button in your application, and more important don't bother the user with a dialog when he presses back, asking if he want to exit. The android system will take care to shut down your app when the user exit with the back key.
- You don't need to call
System.exit(0)
to terminate anActivity
, justActivity.finish()
To avoid an Intent
to call multiple instance of the same Activity
remove the FLAG_ACTIVITY_NEW_TASK
flag from the Intent
and add the FLAG_ACTIVITY_CLEAR_TOP
one.
From Doc:
FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.FLAG_ACTIVITY_NEW_TASK
If set, this activity will become the start of a new task on this history stack.