Super.Onbackpressed() and Finish() Exits the App

super.onBackPressed() and finish() exits the app

Remove

android:noHistory="true" 

from the manifest (PostActivity). A value of noHistory="true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

super.onBackPressed() and finish() exits the app

Remove

android:noHistory="true" 

from the manifest (PostActivity). A value of noHistory="true" means that the activity will not leave a historical trace. It will not remain in the activity stack for the task, so the user will not be able to return to it.

How to exit the android application on backpress?

You need to Override onBackPressed() inside your MainActivity()
and use finishAffinity to finish all activities

onBackPressed()

Called when the activity has detected the user's press of the back key.

finishAffinity() :
added in API level 16

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

SAMPLE CODE

@Override
public void onBackPressed() {
super.onBackPressed();
ActivityCompat.finishAffinity(MainActivity.this);
}

EDIT

I want that the user should be able to exit it from the Main Activity itself by displaying a simple AlertDialog box.

@Override
public void onBackPressed() {

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this)
.setTitle("Alert")
.setCancelable(false)
.setMessage("Are your sure want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.finishAffinity(MainActivity.this);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {

}
});

builder.show();

}

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();
}

clean stack and exit app onBackPressed()

You can do this two way:

  1. kill app by using android.os.Process.killProcess(android.os.Process.myPid()); on back press.

for this you need to add below permission on manifest.

<uses-permission
android:name="android.permission.KILL_BACKGROUND_PROCESSES" />

2 . use static boolean 'isKill' variable with default false and every time set false in login activity oncreate() method.

set isKill value true in login activity onBackPress() method.

And then write below code in every activity onResume() method

if(isKill)
{
finish();
}

Back button exits the app instead of going back to MainActivity

Remove meta data from second activity

<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="full_package_name.MainActivity" />

Remove it

<activity
android:name=".question.QuestionActivity"></activity>

is enough

and make sure while you write startActivity(intent) you do not add finish() after it

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.



Related Topics



Leave a reply



Submit