Question

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

`

Was it helpful?

Solution

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

OTHER TIPS

Simply finish() all of your activities

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.

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

@Override
public void onBackPressed() {
        super.onBackPressed();
        this.finish();
}

@Override public void onBackPressed() {

        finish();

    } 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top