Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit an android app

How to close an android app if more than one activity is in active state?

like image 285
includeMe Avatar asked Feb 28 '11 08:02

includeMe


People also ask

How do you exit an app?

You can call System. exit(); to get out of all the acivities.

How do I close an app on back press on Android?

While some app developers use it to close their apps, some use it to traverse back to the app's previous activity. Many apps require the user to press the 'Back' button two times within an interval to successfully close the application, which is considered the best practice.

What does system exit do in Android?

exit. Terminates the currently running Java Virtual Machine. The argument serves as a status code; by convention, a nonzero status code indicates abnormal termination.

How do I close android studio?

How to Close project in Android Studio: Step 1: Click on File then Click on Close Project and your project will be closed.


2 Answers

A blog post entitled Exiting Android Application will show how to exit an Android app:

When the user wishes to exit all open activities, they should press a button which loads the first Activity that runs when your app starts, in my case "LoginActivity".

    Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.putExtra("EXIT", true);
    startActivity(intent);

The above code clears all the activities except for LoginActivity. LoginActivity is the first activity that is brought up when the user runs the program. Then put this code inside the LoginActivity's onCreate, to signal when it should self destruct when the 'Exit' message is passed.

    if (getIntent().getBooleanExtra("EXIT", false)) {
        finish();
    }
like image 75
xydev Avatar answered Oct 19 '22 14:10

xydev


I got an easy solution for this problem

From the activity you press the exit button go to the first activity using the following source code. Please read the documentation for FLAG_ACTIVITY_CLEAR_TOP also.

Intent intent = new Intent(ExitConfirmationActivity.this, FirstActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);

Now overide onResume() of the first activity using finish()

like image 35
includeMe Avatar answered Oct 19 '22 13:10

includeMe