0

In my application, I have a login screen, after successful login, a tab will load with 4 tab activities. I want to exit from the application when I press the back button. I have tried this. But not working:`

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

          android.os.Process.killProcess(android.os.Process.myPid());

     super.onKeyDown(keyCode, event);
     return true;
    }
    return false;
}

`

irfan
  • 869
  • 3
  • 12
  • 25

5 Answers5

2

Simply finish() all of your activities

2

Simply calling finish(). That doesn't mean, though, that the app will really exit in the same instant of its execution, it just says Android SO that you want to terminate and Android will when it decides, so don't surprise that after you call that function there still remains threads or other stuff running.

nKn
  • 13,691
  • 9
  • 45
  • 62
2

I have a login screen, after successful login, a tab will load with 4 tab activities.

=>

Step 1:

You should finish login activity while starting up tab activity.

For example:

Intent intent = new Intent(this, MyTabActivity.class);
startActivity(intent);
finish();

Step 2:

Now, if you call finish() inside onBackPressed() of your tab activity then it will come out from your app.

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}
Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
  • It work only if application starts main activity then if we press back button it will close the application. I tried same code but it close main activity but it open last activity again. – Srihari Jul 11 '14 at 10:09
1

Override onBackPressed() instead and call finish to kill the activity.

@Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}
Aashir
  • 2,621
  • 4
  • 21
  • 37
1

@Override public void onBackPressed() {

        finish();

    } 
Dakshesh Khatri
  • 639
  • 7
  • 12