In my application, i have exit button to exit from the application. So i have finish()
all the activities. But i loses h/w back button behaviour. So my question is: I want to go back to all activites and also want to exit when clicking on exit button..
Asked
Active
Viewed 1,121 times
0

dreamcoder
- 1,233
- 1
- 11
- 25
-
didn't get you problem.. – asish Apr 18 '12 at 08:52
-
possible duplicate of [Close application?](http://stackoverflow.com/questions/2042222/close-application) – Dharmendra Apr 18 '12 at 09:01
-
@ashish please see the question again..i have edited – dreamcoder Apr 18 '12 at 09:02
5 Answers
4
your question is little bit confusing but i think you need this
Finish all activities when back button is pressed
and never use system.exit for exiting application Close application and launch home screen on Android
edited
editButton.setOnClickListener(new View.OnCLickListener()
{
public void onClick(View v) {
finish();
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
}
});
-
Nope..I want to back all activites but when clicking on exit it should exit the app.. – dreamcoder Apr 18 '12 at 09:00
-
will application exit on clicking exit button? as you are recreating intent? – dreamcoder Apr 18 '12 at 09:13
-
yes it will take you out from application and also clear your activity stack because in it we have added flag FLAG_ACTIVITY_CLEAR_TOP – vipin Apr 18 '12 at 09:16
0
you may want to override onBackPressed()
method and call finish()
inside it

julian
- 381
- 1
- 7
-
No..it will still come out of application.. i want to move to back page and also want to exit from application when i click on exit button.. – dreamcoder Apr 18 '12 at 08:58
-
Can you please describe the scenario? I don't get the question either, sorry for that. – julian Apr 18 '12 at 09:09
0
Use below code ...I hope it work
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(1);
ComponentName componentInfo = taskInfo.get(0).topActivity;
am.restartPackage(componentInfo.getPackageName());
0
/**easiest way that I found and working. Setting up onclick="exitBtn"
for button in XML file. and defining that button in java*/
public void exitBtn(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(OderActivity.this); // this will pop up a dialog box for OK or Cancel//
builder.setMessage("Do you want to exit");
builder.setCancelable(true);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}

Omal Perera
- 2,971
- 3
- 21
- 26

Freaky NT
- 1
- 3
-
if u don't want to include the dialog box, then u can remove all the alert dialog codes and simply put from intent to finish() lines from the above code. – Freaky NT Jul 03 '17 at 19:18