I have activities like A->B->C->D. How can I close the A activity if I have 4 activities on my stack? Also later when I open activity E i want B to be closed aswell, so I want to have C->D->E only.
There is no such as a direct way to manage activities number in stack. So far I know that stack is big as much as available memory.
Also consider LaunchMode and whether activities are in the same task or not.
So, you might implement your own Activity manager to finish un-wanted activities.
Here is briefly how I see the solution:
ActivityItem
ActivityItem
in your custom Application. To avoid memory leak, use WeakReference
. create a public setter adActivity
to add and manage activitiesadActivity
and pass current activity.adActivity
job is firstly clean the list of destroyed activities. thanks WeakReference
. Then manually kill older activities before last 3 items in your list. It's not easy as it looks.. for example: SingleInstance
and rotation will make it challenge to achieve this :-)that is it.
Good luck,'.