1

I want to end the whole application when user click back button in android.Currently It again go to previous opened activity. I also try override onBackPressed() but it doesnt work.

public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        onBackPressed();
    }
       return super.onKeyDown(keyCode, event);
}
enter code here
@Override
public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();
    return;
}
shivani
  • 11
  • 2
  • 1
    When you say you overrode it ... You do realize your implementation does nothing? Like you just called through to the parent implementation ... which justs goes back to the previous activity. If you want to end the application – Greg Giacovelli Oct 13 '12 at 06:27

5 Answers5

4

Try this, start your application from a Activity that will act as your Root.

public class Root extends Activity {

     public void onCreate() {
         super.onCreate();
         handleIntent();
      }

      public void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent();
      }

     private void handleIntent() {
         boolean end = getIntent.getBooleanExtra("End");
         if (end) {
            finish();
         } else {
            Intent intent = new Intent(this, MyRealFirstActivity.class); // where this is the class that you want the user to see when they launch your app.
            startActivity(intent);
         }
      }



      public void onResume() {
        super.onResume();
        finish();
      }
}

Then inside the activity that you want the back button to end the app, do the following:

public class LastActivity extends Activity {

    public void onBackPressed() {
        Intent intent = new Intent(this, Root.class);
        intent.putExtra("End", true);
        intent.addFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }
}

This way all the activities that were started in between you launching the app and then hitting the back button, will all be finished() for you. This essentially will clean the slate of the app from start to finish and then exit.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • This kind of makes sense, however I though onResume is always called when activity comes back to foreground. Imagine you're in your root activity when you receive a phone call. When the call ends, your root activity is brought back to the foreground, onResume is called - and the activity finishes. Not very good, is it? – Aleks G Jan 15 '13 at 16:33
  • But with that same logic, onResume was already called ... so the activity would have been finished already. Resuming would not happen, it would be at the activity that was started immediately after. – Greg Giacovelli Jan 15 '13 at 21:37
2

In this case you need to finish activity, so your current activity will be closed by using finish() method. but you should also write finish() in each and every previous activities, where you call intent(start another activity). Hope it makes clear.

jay
  • 292
  • 1
  • 11
1
 public void onBackPressed() {

         Intent intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_HOME);
         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         this.finish();
         startActivity(intent);
       }
Zombie
  • 1,965
  • 2
  • 20
  • 34
1

Here we have two methods to finish or kill the app on back button pressed.

  1. Using finish() which closes or finishes the current activity on android screen.

    for example : you have 2 activities i.e A & B. You ll go from A activity to B activity using intent, now foreground acitivity is B, you want to go back & kill B activity & goto A acitivity use finish() onclick of backbutton. If your on A activity then it closes the app. see below code.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    finish();
    }
    return super.onKeyDown(keyCode, event);
    }
    
  2. Using android.os.Process.killProcess(android.os.Process.myPid()); which kills the app i.e force close the app & goto the home screen.see below code.

    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
    // Kills & force closes the app 
    android.os.Process.killProcess(android.os.Process.myPid());
    }
    return super.onKeyDown(keyCode, event);
    }
    
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
1

Here's the way I did it in my app: ActivityA is the first one to start; it in turn starts ActivityB. In ActivityB I may encounter an error, in which case I Want to return to activityA, or everything may finish correctly, in which case I want to "finish" the app altogether.

In ActivityA:

public class ActivityA extends Activity {
...
    private final static int REQUEST_ACT_B = 1;
...
    private void startChild() {
        startActivityForResult(new Intent(this, ActivityB.class), REQUEST_ACT_B;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_TASK && resultCode == ActivityB.RESULT_OK) {
            this.finish();
        }
    }
}

And then in ActivityB:

public class ActivityB extends Activity {
...
    public final static int RESULT_OK = 1;
    public final static int RESULT_ERROR = 2;
...
    private void finishWithError() {
        setResult(RESULT_ERROR);
        finish();
    }

    private void finishSuccessfully() {
        setResult(RESULT_OK);
    }
}

Essentially, ActivityA starts ActivityB and expects a result back. If it receives back result "OK", then it closes itself and the user is none the wiser: the app has finished. If it received result "error", then ActivityA stays open.

Aleks G
  • 56,435
  • 29
  • 168
  • 265