How to clear specific activity from the stack history?

I’m not sure you can directly programmatically remove activities from the history, but if you use startActivityForResult() instead of startActivity(), then depending on the return value from your activity, you can then immediately finish() the predecessor activity to simulate the behaviour you want. By using this method in all your activities, you can have this behaviour cascading the activity stack to allow you to go from activity D to activity A.

I know this isn’t your situation, but in future if you know before you start the activity that you don’t want the predecessor to remain, you can call finish() immediately after startActivity().

Please see the section called “Lifetime of the New Screen” in Common Tasks and How to do Them in Android

Leave a Comment