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.
Asked
Active
Viewed 99 times
1
-
2try this http://stackoverflow.com/questions/1898886/removing-an-activity-from-the-history-stack – D.J Nov 03 '16 at 12:08
-
@DheerubhaiBansal I have already read it, but it closes all the activities from the stack. In my case I want to keep only the latest 3 and close all the rest – Unique LoL Apps Nov 03 '16 at 12:09
-
If you want to have a max of 3 `Activity` objects alive, then you have to manage them yourself. Have a Singleton to hold their refferences, and call finish when adding a new one: `Singleton.add(Activity newActivity){if (arrayList.size()>3){Activity dying = ArrayList.get(0); dying.finish(); ArrayList.remove(0); }` then, on each Activity.onCreate() -> `Singleton.add(this);` – Bonatti Nov 03 '16 at 12:34
-
alright, thanks for your comment, I will be reading more about Singleton right now. Thanks I'll try it and I'll tell you if it works. – Unique LoL Apps Nov 03 '16 at 12:35
-
Singleton is an instance that is the same thoughout the application... it just means something such as `public class Example {private static Example ourInstance = new Example();public static Example getInstance() {return ourInstance;} private Example() {}}` – Bonatti Nov 03 '16 at 12:58
-
Create a global Arraylist of type Activity (Like ArrayList
myArrayList) and add every activity in this list when oncreate is called... Now iterate for loop and call finish() method to finish that activity up to your single last activity, try this one and let me know if you are not getting it or if it works for you.... – Bhavnik Nov 03 '16 at 13:23 -
I'd just recommend not using Activities at all, and using Flow instead. – EpicPandaForce Nov 03 '16 at 14:21
-
@Bonatti your approach using Singleton is a bit complicated to me beacsue it uses static, and in my case all my activities are exactly the same Class Activity but with different values. – Unique LoL Apps Nov 03 '16 at 22:32
-
@Bhavnik your approach is pretty smart and it solved my problem, the global ArrayList stores some memory because of these activites. Also can I use these
objects to start the old activity to the front instead of killing it? I can't use Intent and addFlag because these are no classes but an activites – Unique LoL Apps Nov 03 '16 at 22:51
3 Answers
0
When you start activity use
startActivityForResult(intent,code);//different code for all activity
and call
finishActivity(code);//which activity do you want to close?

D.J
- 1,439
- 1
- 12
- 23
-
that was my first thought as I asked it yesterday http://stackoverflow.com/questions/40391270/finish-an-activity-that-is-back-in-the-stack but it is not working :( – Unique LoL Apps Nov 03 '16 at 12:20
0
You can call:
finish();
after:
startActivity(intent);
In every activity that you want to close, when you open another activity.

Rodrigo Paixão
- 246
- 5
- 12
-
yes but I want to keep the latest 3, with this approach I will have only 1 activity. – Unique LoL Apps Nov 03 '16 at 12:33
-
Even if you only start the activity C->D->E whitout the `finish()`? – Rodrigo Paixão Nov 03 '16 at 12:37
-
i can't use that approach using finish() because that won't keep my latest 3 activities – Unique LoL Apps Nov 03 '16 at 12:58
-
-
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { //replaces the default 'Back' button action if(keyCode== KeyEvent.KEYCODE_BACK) { Intent intent = new Intent(PhotoViewerActivity.this, MainActivity.class); startActivity(intent); finish(); } return true; } – Rodrigo Paixão Nov 03 '16 at 13:04
-
You can put this code on D and E activities... to return to the previews activity. Not Keeping they open, but reopen then. – Rodrigo Paixão Nov 03 '16 at 13:06
0
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:
- Create a model to store activity, its index in stack, date ..i.e.
ActivityItem
- Create an empty List of
ActivityItem
in your custom Application. To avoid memory leak, useWeakReference
. create a public setteradActivity
to add and manage activities - Better approach to create activity base class and reuse it as superClass wherever you want to manage count of running activities. instead of repeating same implementation for each different activity.
- OnCreate in your base Activity call
adActivity
and pass current activity. adActivity
job is firstly clean the list of destroyed activities. thanksWeakReference
. 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,'.

Maher Abuthraa
- 17,493
- 11
- 81
- 103
-
@Bhvanik solutoin solved my problem and it's very very easy to implement. When I will have some free time I will try to give a try on your soltion. Thank you for taking your time to write your answer. – Unique LoL Apps Nov 03 '16 at 22:53
-
1I think both solutions are too close .. just be careful on not falling into memory leak .. keeping references of activities leads to block them from being removed by garbage collector even if they were destroyed. see http://android-developers.blogspot.cz/2009/01/avoiding-memory-leaks.html – Maher Abuthraa Nov 03 '16 at 23:30