1

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.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Unique LoL Apps
  • 88
  • 1
  • 11
  • 2
    try 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 Answers3

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
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:

  1. Create a model to store activity, its index in stack, date ..i.e. ActivityItem
  2. Create an empty List of ActivityItem in your custom Application. To avoid memory leak, use WeakReference. create a public setter adActivity to add and manage activities
  3. 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.
  4. OnCreate in your base Activity call adActivity and pass current activity.
  5. 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,'.

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
  • 1
    I 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