Search code examples
androidandroid-activityback-buttonactivity-finish

finish caller activity when starting another one


enter image description here like in the diagram I want to close all previous activities when starting another one. I tried a lot with flags, but it doesn't seems to work. eg. when starting Activity B on Activity A I called

Intent intent = new Intent(this, ActivityB.class);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

or

Intent intent = new Intent(this, ActivityB.class);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent = intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

But the finish() Method isn't called in Activity A. I've got a global class, called Navigation which extends Activity to handle things like this in one class. All orange Activities extends my Navigation, the blue ones don't. BackButton behaviour seems to work fine, but the finish() method isn't called when starting another activity. Do I have to explicitly call finish()? Where? If I do it in onStop I can't go back to Activity X when restarting the app.


Solution

  • After

    startActivity(intent);
    

    simply put

    finish();
    

    in activity that you want to finish. Flags are needed only when ie. you want to clear activity stack