How to clear intermediate activities in Android?

24 views Asked by At

Flows for my screens are:

Activities: AppHome -> A - > B -> C -> Z

Trying to solve use case: When I create the activity Z from C -> finish activities C & B.

Expectation: Pressing back button from Z should land user on Activity A.

My solutions (which did not work)

Launch Activity B in a new stack (use Intent.FLAG_ACTIVITY_NEW_TASK). When launching activity Z, use finishAffinity() on Activity C. Expecting that the new stack will be gone leaving behind previous stack: Home -> Activity A. However, pressing back button from Z closes the App.

I also tried to use finishAndRemoveTask() on Activity C when launching Z but seems like doing nothing. Any suggestions?

PS: I don't want to finish B when jumping to C since need to provide back navigation there.

Thanks!

1

There are 1 answers

0
Viewed On

One of the possible way it is uses navigation component from androidx. You will have backstack with activities and fragments that allow you to control where return when back button pressed via popup methods to previous destination in the graph.