Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android, Finish all activities

Tags:

android

I need to finish all the activities running in an Android application when an exit button is clicked. How can I do that?

What I want:

  1. An option menu on screen with an EXIT option.
  2. When I click the Exit menu, the application should close.
like image 238
jennifer Avatar asked Jan 21 '11 11:01

jennifer


People also ask

How do you close all activities?

Delete all activity On your Android phone or tablet, go to myactivity.google.com. Above your activity, tap Delete .

What does finish () do in Android?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How do you finish previous activity?

Navigate to app>java>your app's package name>Right click on it>New>Empty Activity>Name it as >Main2Activity and click on Finish to create a new activity. Navigate to app > java > your app's package name > MainActivity file and add the code below. Comments are added in the code to get to know in detail.


1 Answers

I don't understand the people who say that this should not be done. Closing an activity can be done quite easily using the built in method to do so.

There is no place in the official documentation that says this should not be done.

For those who think it should not be done for any purpose, maybe you can't think of a reason to do so. There are plenty of legitimate reasons to do this. And since it is a method built in to the Android code.. Google has also decided that you might need to depending on your use. Not all of us only create consumer applications for Android.

So to accomplish this task..shutting down all of the activities in your stack

public void quitApp(){
      this.finishAffinity();
}

I created a kiosk app that was run as the default launcher. So I needed a way to exit the app to get to settings or other apps on the device. So in an admin Activity, I placed a pin number pad.. after the user inputs the correct pin, the app needed to exit to the original launcher. I used above code.

like image 65
user2914118 Avatar answered Oct 04 '22 08:10

user2914118