4

I have an android application that has 3 activites. For the 1st and 2nd activity I want the back button to exit all existing activities.

At the moment the back button is exiting the activity it is initiated on but if its pressed on the 2nd activity, the 1st activity will be displayed, rather than exiting the application as the 1st activity leads on to the 2nd.

The reason I require this functionality is because the 1st and 2nd activities use chronometer timers which continue to run when the HOME button is presssed which I want. But I need a way to reset the timers to fully exit the application using the BACK button.

Here is my code for the back button which in present in both the 1st and 2nd activities.

@Override
 public void onBackPressed() { // method for exit confirmation
  AlertDialog.Builder builder = new AlertDialog.Builder(BreakActivity.this);
  builder.setMessage("Are you sure you want to exit?")
         .setCancelable(false)
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  BreakActivity.this.finish();
             }
         })
         .setNegativeButton("No", new DialogInterface.OnClickListener() {
             public void onClick(DialogInterface dialog, int id) {
                  dialog.cancel();
             }
         });
  AlertDialog alert = builder.create();
  alert.show();

        }         
  };  

I have researched the possibility of using the following code:

intent = new Intent(this, FinActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent)
finish();

However, I'm not sure where this should be implemented and I really only want to end all the applications if 'Yes' is selected after the confirmation message is displayed after the BACK button is pressed (see first code snippet).

Any help in getting this working would be greatly appreciated!

Thank you

Rob
  • 1,479
  • 5
  • 18
  • 24

5 Answers5

11

@Rob when you are navigating from activity 1 to 2 and 2 to 3 with intent use finish function

like

 i=new Intent(Main_Menu.this, ABC.class);
          finish();
         startActivity(i);

It will kill the activity from which you will go to next activity and then when you press the backbutton it will take you outside the application , because there will be no activity in the stack

Avi Kumar
  • 4,403
  • 8
  • 36
  • 67
  • Hi thanks for this reply. I like the sound of this but is ABC the current activity? and what is Main_menu? I'm getting main_menu cannot be resolved to a type? – Rob Apr 05 '12 at 13:26
  • Sorry main_menu is one of activities in my application and abc is the activity called , like moving from first activity to second activity , here Main_Menu is the current Activity – Avi Kumar Apr 05 '12 at 13:28
  • Ok thanks I got it working! This works better for my application but the answer below is also a viable solution, thanks all – Rob Apr 05 '12 at 13:44
7

you can use this

        finish();
        Intent intent = new Intent(Intent.ACTION_MAIN); 
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        intent.addCategory(Intent.CATEGORY_HOME); 
        startActivity(intent);

note one thing always avoid system.exit to quit from application it is not recomended and may caus various problems

https://groups.google.com/forum/?fromgroups#!topic/android-developers/Y96KnN_6RqM

Close application and launch home screen on Android

Community
  • 1
  • 1
vipin
  • 2,851
  • 4
  • 18
  • 32
0

Use this line of code:

System.exit(1);

//The integer 1 is just a return code.

how to close whole application in android

Community
  • 1
  • 1
Ravi1187342
  • 1,247
  • 7
  • 14
0

You can use-

Process.killProcess(android.os.Process.myPid());
Kanwaljit Singh
  • 4,339
  • 2
  • 18
  • 21
Jiang Qi
  • 4,450
  • 3
  • 19
  • 19
0

Don't call any System.exit or something else! Android architecture is built to self managment of processes. Android itself decides when to kill app process. When you killing your application by hand you are creating additional overhead for user device.

Try to manually reset your timers and revise your app architecture.

pepyakin
  • 2,217
  • 19
  • 31