• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Which is the best method to exit from the application?

Thayar

Newbie
I have more than one activities .if i click an exit button ,I have to exit from my application.
I have searched the google i got two options.Which is right method ?
First method:

Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(intent); And override the onResume of initial activity with a finish();

Second method:

closing all activities one by one .
 
I have more than one activities .if i click an exit button ,I have to exit from my application.

I hate to be this guy but may I ask why you need to exit your application? In most cases you should not need an exit button and let Android do the rest. Here is a good write-up from Reto Meier: The Radioactive Yak: When to Include an Exit Button in Android Apps (Hint: Never)

I have learned a lot from that guy alone with all of his books.

In short, if you need an exit button finish() will suffice.
 
The activities that are not main, call finish() in the onPause() or onStop() method. This will terminate the active activity and go to the previous activity. While on the main activity, set the onclick method for the button to just call finish(). In theory this should work, just save everything that needs to be saved in onPause or onStop and then call finish(). This way everything gets saved, the new activity opens, and the old one closes, and when "back button" is pushed, it should go to the main activity.
 
Back
Top Bottom