How to close Android application?

Android

Android Problem Overview


I want to close my application, so that it no longer runs in the background.

How to do that? Is this good practice on Android platform?

If I rely on the "back" button, it closes the app, but it stays in background. There is even application called "TaskKiller" just to kill those apps in the background.

Android Solutions


Solution 1 - Android

Android has a mechanism in place to close an application safely per its documentation. In the last Activity that is exited (usually the main Activity that first came up when the application started) just place a couple of lines in the onDestroy() method. The call to System.runFinalizersOnExit(true) ensures that all objects will be finalized and garbage collected when the the application exits. You can also kill an application quickly via android.os.Process.killProcess(android.os.Process.myPid()) if you prefer. The best way to do this is put a method like the following in a helper class and then call it whenever the app needs to be killed. For example in the destroy method of the root activity (assuming that the app never kills this activity):

Also Android will not notify an application of the HOME key event, so you cannot close the application when the HOME key is pressed. Android reserves the HOME key event to itself so that a developer cannot prevent users from leaving their application. However you can determine with the HOME key is pressed by setting a flag to true in a helper class that assumes that the HOME key has been pressed, then changing the flag to false when an event occurs that shows the HOME key was not pressed and then checking to see of the HOME key pressed in the onStop() method of the activity.

Don't forget to handle the HOME key for any menus and in the activities that are started by the menus. The same goes for the SEARCH key. Below is some example classes to illustrate:

Here's an example of a root activity that kills the application when it is destroyed:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

public class HomeKey extends CustomActivity {

	public void onDestroy() {
		super.onDestroy();

		/*
		 * Kill application when the root activity is killed.
		 */
		UIHelper.killApp(true);
	}

}

Here's an abstract activity that can be extended to handle the HOME key for all activities that extend it:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 */

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;

/**
 * Activity that includes custom behavior shared across the application. For
 * example, bringing up a menu with the settings icon when the menu button is
 * pressed by the user and then starting the settings activity when the user
 * clicks on the settings icon.
 */
public abstract class CustomActivity extends Activity {
	public void onStart() {
		super.onStart();

		/*
		 * Check if the app was just launched. If the app was just launched then
		 * assume that the HOME key will be pressed next unless a navigation
		 * event by the user or the app occurs. Otherwise the user or the app
		 * navigated to this activity so the HOME key was not pressed.
		 */

		UIHelper.checkJustLaunced();
	}

	public void finish() {
		/*
		 * This can only invoked by the user or the app finishing the activity
		 * by navigating from the activity so the HOME key was not pressed.
		 */
		UIHelper.homeKeyPressed = false;
		super.finish();
	}

	public void onStop() {
		super.onStop();

		/*
		 * Check if the HOME key was pressed. If the HOME key was pressed then
		 * the app will be killed. Otherwise the user or the app is navigating
		 * away from this activity so assume that the HOME key will be pressed
		 * next unless a navigation event by the user or the app occurs.
		 */
		UIHelper.checkHomeKeyPressed(true);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.settings_menu, menu);

		/*
		 * Assume that the HOME key will be pressed next unless a navigation
		 * event by the user or the app occurs.
		 */
		UIHelper.homeKeyPressed = true;

		return true;
	}

	public boolean onSearchRequested() {
		/*
		 * Disable the SEARCH key.
		 */
		return false;
	}
}

Here's an example of a menu screen that handles the HOME key:

/**
 * @author Danny Remington - MacroSolve
 */

package android.example;

import android.os.Bundle;
import android.preference.PreferenceActivity;

/**
 * PreferenceActivity for the settings screen.
 * 
 * @see PreferenceActivity
 * 
 */
public class SettingsScreen extends PreferenceActivity {
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		addPreferencesFromResource(R.layout.settings_screen);
	}

	public void onStart() {
		super.onStart();

		/*
		 * This can only invoked by the user or the app starting the activity by
		 * navigating to the activity so the HOME key was not pressed.
		 */
		UIHelper.homeKeyPressed = false;
	}

	public void finish() {
		/*
		 * This can only invoked by the user or the app finishing the activity
		 * by navigating from the activity so the HOME key was not pressed.
		 */
		UIHelper.homeKeyPressed = false;
		super.finish();
	}

	public void onStop() {
		super.onStop();

		/*
		 * Check if the HOME key was pressed. If the HOME key was pressed then
		 * the app will be killed either safely or quickly. Otherwise the user
		 * or the app is navigating away from the activity so assume that the
		 * HOME key will be pressed next unless a navigation event by the user
		 * or the app occurs.
		 */
		UIHelper.checkHomeKeyPressed(true);
	}

	public boolean onSearchRequested() {
		/*
		 * Disable the SEARCH key.
		 */
		return false;
	}

}

Here's an example of a helper class that handles the HOME key across the app:

package android.example;

/**
 * @author Danny Remington - MacroSolve
 *
 */

/**
 * Helper class to help handling of UI.
 */
public class UIHelper {
	public static boolean homeKeyPressed;
	private static boolean justLaunched = true;

	/**
	 * Check if the app was just launched. If the app was just launched then
	 * assume that the HOME key will be pressed next unless a navigation event
	 * by the user or the app occurs. Otherwise the user or the app navigated to
	 * the activity so the HOME key was not pressed.
	 */
	public static void checkJustLaunced() {
		if (justLaunched) {
			homeKeyPressed = true;
			justLaunched = false;
		} else {
			homeKeyPressed = false;
		}
	}

	/**
	 * Check if the HOME key was pressed. If the HOME key was pressed then the
	 * app will be killed either safely or quickly. Otherwise the user or the
	 * app is navigating away from the activity so assume that the HOME key will
	 * be pressed next unless a navigation event by the user or the app occurs.
	 * 
	 * @param killSafely
	 *            Primitive boolean which indicates whether the app should be
	 *            killed safely or quickly when the HOME key is pressed.
	 * 
	 * @see {@link UIHelper.killApp}
	 */
	public static void checkHomeKeyPressed(boolean killSafely) {
		if (homeKeyPressed) {
			killApp(true);
		} else {
			homeKeyPressed = true;
		}
	}

	/**
	 * Kill the app either safely or quickly. The app is killed safely by
	 * killing the virtual machine that the app runs in after finalizing all
	 * {@link Object}s created by the app. The app is killed quickly by abruptly
	 * killing the process that the virtual machine that runs the app runs in
	 * without finalizing all {@link Object}s created by the app. Whether the
	 * app is killed safely or quickly the app will be completely created as a
	 * new app in a new virtual machine running in a new process if the user
	 * starts the app again.
	 * 
	 * <P>
	 * <B>NOTE:</B> The app will not be killed until all of its threads have
	 * closed if it is killed safely.
	 * </P>
	 * 
	 * <P>
	 * <B>NOTE:</B> All threads running under the process will be abruptly
	 * killed when the app is killed quickly. This can lead to various issues
	 * related to threading. For example, if one of those threads was making
	 * multiple related changes to the database, then it may have committed some
	 * of those changes but not all of those changes when it was abruptly
	 * killed.
	 * </P>
	 * 
	 * @param killSafely
	 *            Primitive boolean which indicates whether the app should be
	 *            killed safely or quickly. If true then the app will be killed
	 *            safely. Otherwise it will be killed quickly.
	 */
	public static void killApp(boolean killSafely) {
		if (killSafely) {
			/*
			 * Notify the system to finalize and collect all objects of the app
			 * on exit so that the virtual machine running the app can be killed
			 * by the system without causing issues. NOTE: If this is set to
			 * true then the virtual machine will not be killed until all of its
			 * threads have closed.
			 */
			System.runFinalizersOnExit(true);

			/*
			 * Force the system to close the app down completely instead of
			 * retaining it in the background. The virtual machine that runs the
			 * app will be killed. The app will be completely created as a new
			 * app in a new virtual machine running in a new process if the user
			 * starts the app again.
			 */
			System.exit(0);
		} else {
			/*
			 * Alternatively the process that runs the virtual machine could be
			 * abruptly killed. This is the quickest way to remove the app from
			 * the device but it could cause problems since resources will not
			 * be finalized first. For example, all threads running under the
			 * process will be abruptly killed when the process is abruptly
			 * killed. If one of those threads was making multiple related
			 * changes to the database, then it may have committed some of those
			 * changes but not all of those changes when it was abruptly killed.
			 */
			android.os.Process.killProcess(android.os.Process.myPid());
		}

	}
}

Solution 2 - Android

YES! You can most certainly close your application so it is no longer running in the background. Like others have commented finish() is the Google recommended way that doesn't really mean your program is closed.

System.exit(0);

That right there will close your application out leaving nothing running in the background.However,use this wisely and don't leave files open, database handles open, etc.These things would normally be cleaned up through the finish() command.

I personally HATE when I choose Exit in an application and it doesn't really exit.

Solution 3 - Android

This is the way I did it:

I just put

Intent intent = new Intent(Main.this, SOMECLASSNAME.class);
Main.this.startActivityForResult(intent, 0);

inside of the method that opens an activity, then inside of the method of SOMECLASSNAME that is designed to close the app I put:

setResult(0);
finish();

And I put the following in my Main class:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	if(resultCode == 0) {
		finish();
	}
}

Solution 4 - Android

Just to answer my own question now after so much time (since CommonsWare commented on the most popular answer telling we should NOT do this):

When I want to quit the app:

  1. I start my first activity (either splash screen, or whatever activity is currently at the bottom of the activity stack) with FLAG_ACTIVITY_CLEAR_TOP (which will quit all the other activities started after it, which means - all of them). Just make to have this activity in the activity stack (not finish it for some reason in advance).
  2. I call finish() on this activity

This is it, works quite well for me.

Solution 5 - Android

Just write this code on your button EXIT click.

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

And in the onCreate() method of your MainActivity.class write below code as a first line,

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

Solution 6 - Android

It's not possible using the framework APIs. It's at the discretion of the operating system (Android) to decide when a process should be removed or remain in memory. This is for efficiency reasons: if the user decides to relaunch the app, then it's already there without it having to be loaded into memory.

So no, it's not only discouraged, it's impossible to do so.

Solution 7 - Android

For exiting app ways:

Way 1 :

call finish(); and override onDestroy();. Put the following code in onDestroy():

System.runFinalizersOnExit(true)

or

android.os.Process.killProcess(android.os.Process.myPid());

Way 2 :

public void quit() {
    int pid = android.os.Process.myPid();
    android.os.Process.killProcess(pid);
    System.exit(0);
}

Way 3 :

Quit();

protected void Quit() {
    super.finish();
}

Way 4 :

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

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

Way 5 :

Sometimes calling finish() will only exit the current activity, not the entire application. However, there is a workaround for this. Every time you start an activity, start it using startActivityForResult(). When you want to close the entire app, you can do something like the following:

setResult(RESULT_CLOSE_ALL);
finish();

Then define every activity's onActivityResult(...) callback so when an activity returns with the RESULT_CLOSE_ALL value, it also calls finish():

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	switch(resultCode){
	    case RESULT_CLOSE_ALL:{
		    setResult(RESULT_CLOSE_ALL);
            finish();
        }
	}
	super.onActivityResult(requestCode, resultCode, data);
}

Solution 8 - Android

This is how Windows Mobile has worked for... well... ever! Here's what Microsoft have to say on the matter:

http://blogs.msdn.com/windowsmobile/archive/2006/10/05/The-Emperor-Has-No-Close.aspx (is it sad that I remembered the title of the blog post all the way from 2006? I found the article on Google by searching "the emperor has no close" lol)

In short:

> If the system needs more memory while > the app is in the background, it’ll > close the app. But, if the system > doesn’t need more memory, the app will > stay in RAM and be ready to come back > quickly the next time the user needs > it.

Many comments in this question at O'Reilly suggest that Android behaves in much the same way, closing applications that haven't been used for a while only when Android needs the memory they're using.

Since this is a standard feature, then changing the behavior to forcefully close would be changing the user experience. Many users would be used to the gentle dismissal of their Android apps so when they dismiss one with the intention of returning to it after performing some other tasks, they may be rather frustrated that the state of the application is reset, or that it takes longer to open. I would stick with the standard behavior because it is what is expected.

Solution 9 - Android

Calling the finish() method on an Activity has your desired effect on that current activity.

Solution 10 - Android

Copy below code and paste AndroidManifest.xml file in under First Activity Tag.

<activity                        
            android:name="com.SplashActivity"
            android:clearTaskOnLaunch="true" 
            android:launchMode="singleTask"
            android:excludeFromRecents="true">              
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"
                />
            </intent-filter>
        </activity>     

Also Add this below code in all under Activity Tag in AndroidManifest.xml file

 android:finishOnTaskLaunch="true"

Solution 11 - Android

none of all above answers working good on my app

here is my working code

on your exit button:

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
mainIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
mainIntent.putExtra("close", true);
startActivity(mainIntent);
finish();

that code is to close any other activity and bring MainActivity on top now on your MainActivity:

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

Solution 12 - Android

Use "this.FinishAndRemoveTask();" - it closes application properly

Solution 13 - Android

Put a finish(); statement as below:

myIntent.putExtra("key1", editText2.getText().toString());

finish();

LoginActivity.this.startActivity(myIntent);

In every activity.

Solution 14 - Android

@Override
	protected void onPause() {
		super.onPause();
		System.exit(0);
	}

Solution 15 - Android

Simply write the following code in onBackPressed:

@Override
public void onBackPressed() {
    // super.onBackPressed();

    //Creating an alert dialog to logout
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage("Do you want to Exit?");
    alertDialogBuilder.setPositiveButton("Yes",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {
                    Intent intent = new Intent(Intent.ACTION_MAIN);
                    intent.addCategory(Intent.CATEGORY_HOME);
                    startActivity(intent);
                }
            });

    alertDialogBuilder.setNegativeButton("No",
            new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface arg0, int arg1) {

                }
            });

    //Showing the alert dialog
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
}

Solution 16 - Android

Not possible with 2.3. I search alot, and tried many apps. The best solution is to install both (go taskmanager) and (fast reboot). When use them together it will work, and will free the memory. Another option is to upgrade to android ice cream sandwich 4.0.4 which allow control (close) of apps.

Solution 17 - Android

i wanted to return to the home screen of my android device, so i simply used :

moveTaskToBack(true);

Solution 18 - Android

Use of finishAffinity() may be an good option if you want to close all Activity of the app. As per the Android Docs-

Finish this activity as well as all activities immediately below it in the current task that have the same affinity.

Solution 19 - Android

public class CloseAppActivity extends AppCompatActivity
{
    public static final void closeApp(Activity activity)
    {
        Intent intent = new Intent(activity, CloseAppActivity.class);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        finish();
    }
}

and in manifest:

<activity
     android:name=".presenter.activity.CloseAppActivity"
     android:noHistory="true"
     android:clearTaskOnLaunch="true"/>

Then you can call CloseAppActivity.closeApp(fromActivity) and application will be closed.

Solution 20 - Android

by calling finish(); in OnClick button or on menu

> case R.id.menu_settings: > > finish(); > return true;

Solution 21 - Android

I think it will close your activity and all Sub activity related to it.

public boolean onOptionsItemSelected(MenuItem item) {
  
        int id = item.getItemId();]
        if (id == R.id.Exit) {
            this.finishAffinity();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

Solution 22 - Android

The best and shortest way to use the table System.exit.

System.exit(0);

The VM stops further execution and program will exit.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDanailView Question on Stackoverflow
Solution 1 - AndroidDanny Remington - OMSView Answer on Stackoverflow
Solution 2 - AndroidCameron McBrideView Answer on Stackoverflow
Solution 3 - AndroidStephenView Answer on Stackoverflow
Solution 4 - AndroidDanailView Answer on Stackoverflow
Solution 5 - AndroidLalit JawaleView Answer on Stackoverflow
Solution 6 - AndroidMatthiasView Answer on Stackoverflow
Solution 7 - Androidhitesh141View Answer on Stackoverflow
Solution 8 - AndroidAndy EView Answer on Stackoverflow
Solution 9 - Androidr1k0View Answer on Stackoverflow
Solution 10 - AndroidAndroidView Answer on Stackoverflow
Solution 11 - AndroidDaniel SatyaView Answer on Stackoverflow
Solution 12 - AndroidNitika ChopraView Answer on Stackoverflow
Solution 13 - AndroidbeginnerView Answer on Stackoverflow
Solution 14 - AndroidHaris DautovićView Answer on Stackoverflow
Solution 15 - AndroidRamesh RView Answer on Stackoverflow
Solution 16 - AndroidaliView Answer on Stackoverflow
Solution 17 - AndroidAlireza AzadiView Answer on Stackoverflow
Solution 18 - AndroidSanjeet AView Answer on Stackoverflow
Solution 19 - AndroidArtyomView Answer on Stackoverflow
Solution 20 - AndroidKoshView Answer on Stackoverflow
Solution 21 - AndroidLakshay JainView Answer on Stackoverflow
Solution 22 - AndroidRasoul MiriView Answer on Stackoverflow