How to exit from the application and show the home screen?


Question

I have an application where on the home page I have buttons for navigation through the application.

On that page I have a button "EXIT" which when clicked should take the user to the home screen on the phone where the application icon is.

How can I do that?

1
193
3/2/2016 10:03:02 AM

Accepted Answer

Android's design does not favor exiting an application by choice, but rather manages it by the OS. You can bring up the Home application by its corresponding Intent:

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
322
3/11/2012 6:26:24 AM

May be you can try something like this

Suppose in our application, we have a number of activities(say ten) and we need to exit directly from this activity. What we can do is, create an intent and go to the root activity and set flag in the intent as

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

also, add some extra like boolean to the intent

intent.putExtra("EXIT", true);

Then in root activity, check the value of the boolean and according to that call finish(), in the onCreate() of the root activity

if (getIntent().getBooleanExtra("EXIT", false)) {
 finish();
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon