Q:

Close the application instead of going back to previous activity

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.

A:

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

          }
       }
    }

A:

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

