1

i am putting this code in my android activity to handle the back button to get out of the app and stop it but the back button keep move me to the previous activity this is my code , please help me out why it doesn't work :

public boolean onKeyDown(int keyCode, KeyEvent event){
    if(keyCode == KeyEvent.KEYCODE_BACK) {

    System.exit(0);
    return true;
    }
    return super.onKeyDown(keyCode, event);
    }  

and i have mad a debug it get the right value for key code but still it does not work !!

Edit : well what is really happening that the previous activity is a validation activity activity and i am getting device IMEI because if the device is accepted it will show a dialog and proceed to this activity and when the user press back i want him to get out of the app and i don't want the validation process to start all over again !!! ok?

user3534834
  • 85
  • 1
  • 2
  • 7

5 Answers5

1

Do not use System.exit(0). Just call finish() to close the current activity. This is the standard behavior anyway, so I don't see the need for you to override this method at all.

Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • User want exit from app . I.e all previous activity. – Tulsiram Rathod Apr 28 '14 at 10:21
  • Application components have well-defined lifecycles. Use the lifecycle callbacks and manage those lifecycles properly instead of shutting down the app abruptly. Besides, you are changing a fundamental behavior of the operating system, which will be unusual to users who are accustomed to the normal behavior. – Karakuri Apr 28 '14 at 10:23
  • no i have tried it still it gets back to the previous activity !! the previous activity contain a asynctask with connection to web service does this relate to the problem?? – user3534834 Apr 28 '14 at 10:23
  • You should provide more context in your original question. Explain what you are doing and what you want to "stop", and then people can give you guidance on how to do so in a manageable way. – Karakuri Apr 28 '14 at 10:24
0
  @Override
       public boolean onKeyDown(int keyCode, KeyEvent event) {
           if ((keyCode == KeyEvent.KEYCODE_BACK)) {
               AlertDialog.Builder alertbox = new AlertDialog.Builder(MainActivity.this);

               alertbox.setTitle("Do You Want To Exit ?");
               alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) { 
                      // finish used for destroyed activity
                       exit();
                   }
               });

               alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface arg0, int arg1) {
                           // Nothing will be happened when clicked on no button 
                           // of Dialog     
                 }
               });

               alertbox.show();
           }
           return super.onKeyDown(keyCode, event);
       }
    public void exit()
    {
         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
    }
Tushar Narang
  • 1,997
  • 3
  • 21
  • 49
0

Use below snippet while launching this activity

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

and call

finish();

onback press.

Cause your app has some back stack . so by finishing direct will go to previous activity.

You can refer here

Community
  • 1
  • 1
Tulsiram Rathod
  • 1,926
  • 2
  • 19
  • 29
0

insted of key down use below method

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
    }

if you don't want to finish or quit activity then remove super.onBackPressed(); from method.

Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
0

In your validation activity, after you call startActivity for the second activity, call finish(). The validation activity will close and will no longer be in the back stack. You don't need to do any special handling for back presses.

Karakuri
  • 38,365
  • 12
  • 84
  • 104