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?
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
also, add some extra like boolean to the intent
Then in root activity, check the value of the
boolean
and according to that call finish(), in theonCreate()
of the root activityfirst finish your application using method
finish();
and then add below lines in onDestroy for Removing Force close
Here's what i did:
SomeActivity.java
QuitAppActivity.java
Basically what you did is cleared all activities from the stack and launch
QuitAppActivity
, that will finish the task.Some Activities actually you don't want to open again when back button pressed such Splash Screen Activity, Welcome Screen Activity, Confirmation Windows. Actually you don't need this in activity stack. you can do this using=> open manifest.xml file and add a attribute
to these activities.
OR
Sometimes you want close the entire application in certain back button press. Here best practice is open up the home window instead of exiting application. For that you need to override onBackPressed() method. usually this method open up the top activity in the stack.
OR
In back button pressed you want to exit that activity and also you also don't want to add this in activity stack. call finish() method inside onBackPressed() method. it will not make close the entire application. it will go for the previous activity in the stack.
(I tried previous answers but they lacks in some points. For example if you don't do a
return;
after finishing activity, remaining activity code runs. Also you need to edit onCreate with return. If you doesn't run super.onCreate() you will get a runtime error)Say you have
MainActivity
andChildActivity
.Inside ChildActivity add this:
Inside MainActivity's onCreate add this:
It is not recommended to exit your Android Application. See this question for more details.
The user can always quit your app through the home button or in the first activity through the back button.