0

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..

dreamcoder
  • 1,233
  • 1
  • 11
  • 25

5 Answers5

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

       }
});
Community
  • 1
  • 1
vipin
  • 2,851
  • 4
  • 18
  • 32
0
System.exit(1)

may help.. arguement is important.. you can also try this approach..

Community
  • 1
  • 1
ngesh
  • 13,398
  • 4
  • 44
  • 60
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