1

I have the following problem: I opened the activity A, from which I opened the activity B. Then I want to open the activity C from B, such that when pressing the back button in the C activity, the application should be closed instead of going back to the previous (B) activity. I tried to use the ideas from here (http://stackoverflow.com/questions/5794506/android-clear-the-back-stack), but they don't work. I know how to handle intents, I am interested only in the back navigation problem.

strongmayer
  • 488
  • 1
  • 5
  • 14

5 Answers5

6

Start your activities like this..

this in activity A

  int k=1;
            Intent i=new Intent(A.this,B.class);
            startActivityForResult(i,k);

this in activity B

  int j=1;
            Intent i=new Intent(B.this,C.class);
            startActivityForResult(i,j);

in activity C override back button

  @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
    setResult(RESULT_OK, null);
    finish();
}
 return super.onKeyDown(keyCode, event);
}

and put this in activity B

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode ==1) {
          if (resultCode == RESULT_OK) {

            setResult(RESULT_OK, null);
         finish();

          }
       }

and put this in activity A

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
       if (requestCode ==1) {
          if (resultCode == RESULT_OK) {
         finish();

          }
       }
    }
5hssba
  • 8,079
  • 2
  • 33
  • 35
3

Put finish() in onStop() of both Activity A and B. This would close both the Activities, so when you press back button from Activity C , application would exit. This would solve your problem

  public void onStop(){
    super.onStop();
    finish();
    }
Akhil
  • 13,888
  • 7
  • 35
  • 39
1

I recommend strongly against calling System.exit(0);

What you can do is the following. Activity A calls finish() right after StartActivity(B). Similarly, B calls finish() right after startActivity(C). This way, C is the only Activity active, and pressing back stops the app. Or rather, removes it from the stack.

You should not call finish() in onStop(). Call finish() when you start the new Activity, or when processing in the activity has finished.

Btw, finish() doesn't finish your activity immediately. Finish is placed on the stack, and it is executed soon as the current method finishes. Similarly, startActivity is not executed immediately, it is executed when the current method finishes.

Christine
  • 5,617
  • 4
  • 38
  • 61
0

On BackPress you can use

System.exit(0);

to finish the Application.

Parthraj
  • 573
  • 5
  • 16
0

You can use code bellow to go into device home screen.

            Intent startMain = new Intent(Intent.ACTION_MAIN);  
            startMain.addCategory(Intent.CATEGORY_HOME);
            startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(startMain);
Matin Petrulak
  • 1,087
  • 15
  • 23