Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exit android app on back pressed

Tags:

android

People also ask

How do I close an app from pressing the back button?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.

Should Android apps have back button?

Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app's UI.


Try this

public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);

}

Some Activities you don't actually want to open again when the back button is pressed, such as Splash Screen Activity, Welcome Screen Activity and Confirmation Windows. Actually, you don't need this in the activity stack. You can do this using=> open Manifest.xml file and add an attribute

android:noHistory="true"

to these activities.

<activity
    android:name="com.example.shoppingapp.AddNewItems"
    android:label="" 
    android:noHistory="true">
</activity>

OR

Sometimes you want to close the entire application on a certain back button press. Here the best practice is to open up the home window instead of exiting the application. For that, you need to override the onBackPressed() method. Usually, this method opens up the top activity in the stack.

@override
public void onBackPressed(){
Intent a = new Intent(Intent.ACTION_MAIN);
a.addCategory(Intent.CATEGORY_HOME);
a.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(a);
}

OR

On back button pressed you want to exit that activity and also you don't want to add this to the activity stack. Call finish() inside the onBackPressed() method. It will not close the entire application, it will just go to the previous activity in the stack.

onBackPressed() - Called when the activity has detected the user's press of the back key.

public void onBackPressed() {
  finish();
}

You can clear all the back stack doing this.

@Override
public void onBackPressed() {
    finishAffinity();
}

I know its too late, but no one mentioned the code which i used, so It will help others.

this moveTaskToBack work as same as Home Button. It leaves the Back stack as it is.

 public void onBackPressed() {
 //  super.onBackPressed();
    moveTaskToBack();
  }

I found an interesting solution which might help.

I implemented this in my onBackPressed()

finishAffinity();
finish();

FinishAffinity removes the connection of the existing activity to its stack. And then finish helps you exit that activity. Which will eventually exit the application.


If you have more than one screen in history and need all other ones to be ignored. I recommend using this:

@Override
public void onBackPressed() {

    finish();

}