Generally, when the back button is clicked, I call finish()
, and I am taken back to the previous activity
(which is my MenuActivity) :
@Override
public void onBackPressed() {
finish();
}
However, sometimes the there are no other activities
running when the back button is clicked and the app simply exits. Is there a way I can check if an activity
exists or is running in the background/in onPause
state etc...
I would like to write something like this:
@Override
public void onBackPressed() {
if(isActivitiesInStack) //if there are still activities in stack
finish(); //simply call finish and be taken back to next activity
else { // else, there are no acitivites in the stack
Intent intent = new Intent(ThisActivity.this, MenuActivity.class); // then create an intent and send me to the menu acitivy
startActivity(intent);
finish()
}
}