How to Exit When Back Button Is Pressed

How to exit when back button is pressed?

Immediately after you start a new activity, using startActivity, make sure you call finish() so that the current activity is not stacked behind the new one.

EDIT
With regards to your comment:

What you're suggesting is not particularly how the android app flow usually works, and how the users expect it to work. What you can do if you really want to, is to make sure that every startActivity leading up to that activity, is a startActivityForResult and has an onActivityResult listener that checks for an exit code, and bubbles that back. You can read more about that here. Basically, use setResult before finishing an activity, to set an exit code of your choice, and if your parent activity receives that exit code, you set it in that activity, and finish that one, etc...

How to exit an app when Back Button is pressed

Your code (in MainActivity) is incorrect , put finish() after super.onBackPressed()

it must be :

@Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity(); // or finish();
}

Android: Quit application when press back button

When you press back and then you finish your current activity(say A), you see a blank activity with your app logo(say B), this simply means that activity B which is shown after finishing A is still in backstack, and also activity A was started from activity B, so in activity, You should start activity A with flags as

Intent launchNextActivity;
launchNextActivity = new Intent(B.class, A.class);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
launchNextActivity.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(launchNextActivity);

Now your activity A is top on stack with no other activities of your application on the backstack.

Now in the activity A where you want to implement onBackPressed to close the app, you may do something like this,

private Boolean exit = false;
@Override
public void onBackPressed() {
if (exit) {
finish(); // finish activity
} else {
Toast.makeText(this, "Press Back again to Exit.",
Toast.LENGTH_SHORT).show();
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);

}

}

The Handler here handles accidental back presses, it simply shows a Toast, and if there is another back press within 3 seconds, it closes the application.

How to prompt for exit when back button is pressed?

I think the answer is emulating the back button after getting the result of DisplayAlert.

For emulating the back button I found calling Activity.OnBackPressed useful.

For sure it is not the best idea but it is easy to call this method in Shared/PCL Xamarin Forms project in this manner:

  1. Defining a public static Action field somewhere (e.g. the page)
  2. Initializing this field in the MainActivity class by OnBackPressed

So the whole solution would be like this:

Xamarin Forms Page Class

class MyPage : ContentPage
{
public static Action EmulateBackPressed;

private bool AcceptBack;

protected override bool OnBackButtonPressed()
{
if (AcceptBack)
return false;

PromptForExit();
return true;
}

private async void PromptForExit()
{
if (await DisplayAlert("", "Are you sure to exit?", "Yes", "No"))
{
AcceptBack = true;
EmulateBackPressed();
}
}
}

Xamarin Android MainActivity Class

public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
MyPage.EmulateBackPressed = OnBackPressed;

TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

base.OnCreate(bundle);

Xamarin.Forms.Forms.Init(this, bundle);
LoadApplication(new App());
}
}

How to exit the app when the user press back button in navigation component?

You need to clear the navigation stack. You can do it in many ways.

For example, if you know that you only have a single item in your back stack, you can use popBackStack:

navController.popBackStack()
navController.navigate(Destinations.Profile)

Or you can use popUpTo and specify the first destination you wanna pop up, and add inclusive parameter:

navController.navigate(Destinations.Profile) {
popUpTo(Destinations.Login) {
inclusive = true
}
}

How to write a double back button pressed to exit app using flutter

This is an example of my code (I've used "fluttertoast" for showing toast message, you can use snackbar or alert or anything else)

DateTime currentBackPressTime;

@override
Widget build(BuildContext context) {
return Scaffold(
...
body: WillPopScope(child: getBody(), onWillPop: onWillPop),
);
}

Future<bool> onWillPop() {
DateTime now = DateTime.now();
if (currentBackPressTime == null ||
now.difference(currentBackPressTime) > Duration(seconds: 2)) {
currentBackPressTime = now;
Fluttertoast.showToast(msg: exit_warning);
return Future.value(false);
}
return Future.value(true);
}


Related Topics



Leave a reply



Submit