reiley reiley - 4 months ago 16
Android Question

Go to home screen instead of previous Activity

I know this question has been asked many times before, but any of the solution is not working and my situation is a little bit different.

I've an

Activity
which can be called from many different Activities. But I want when the user presses back button, instead of previous activity, app should go to Home screen.

One way to use
StartActivityFromResult()
but then I'll have to use it in every calling Activity.

Answer

you can override onBackPressed method as follows. It should work.

 public void onBackPressed() {
     Intent startMain = new Intent(Intent.ACTION_MAIN);
        startMain.addCategory(Intent.CATEGORY_HOME);
        startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(startMain);

}
Comments