I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should
try this
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
From the screen(B) you want to start the screen(C) with NO back screen, just start this screen (C) with:
screenC.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(screenC);
finish();
The back in screenC will exit the application.
I didn't do any of this. This is how I would revise your code.
the code you use to enter another intent:
Intent whatEverIntentName = new Intent("Path.to.next.Intent");
startActivity(whatEverIntentName);
finish();
This way, you always quit when pressing back. But wait! You can change how you want your back key press to react when pressed.
Do this:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent whatEverIntentName = new Intent("Path.to.the.activity.before.it");
startActivity(whatEverIntentName);
// Don't add finish here.
//This is necessary because you finished your last activity with finish();
}
Intent intent=new Intent(ActivityB.this, ActivityC.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
your activity C will the root activity and on press back will finish the apps.