what is the right way to clear background activity/activites from stack?
Asked Answered
M

3

2

as the questions title says - I need to know what is the best way to "remove"/destroy/finish an activity that are somewhere in the middle of stack and currently on pause mode (not specific instances - but specific derived classes).

for example:
if the current state of the stack looks like this:

ActivityD   <-- top of the stack, currently forground
ActivityC
ActivityA
ActivityC
ActivityA

a request to "clear" all ActivityC instances would cause the stack to be like:

ActivityD  <-- still top of the stack, currently forground.
ActivityA
ActivityA

I don't want to do that depends on activity launch mode or intent flags. I know how to use them and their benefits.

what I currently know I can do is to send broadcast which all activities needed to be destroyed would listen to, and call Activity.finish() when receive the broadcast.
that's working, but it requires receivers to be registered even when their hosting activity is paused, and I'm not sure "finish()" method been called from paused activity is something right to do.

is it right to call Activity.finish() method from resumed activity?

is it right to register receiver int the OnCreate() method, and unregister him OnDestroy()?

is it right to handle broadcast from resumed activity, and call finish() from that point?

is there an "Android way" or some API I don't know about to clear activities from stack?

thanks in advance

Mezoff answered 24/5, 2012 at 11:28 Comment(1)
This is a super old question but I was wondering the same recently. Particularly the ""finish()" method been called from paused activity is something right to do" part. Do you mean "Activity.finish() method from paused activity?" rather than resumed?Baillie
L
2

Make a custom Broadcast receiver and register it in every activity which can be fired on event of your choice. in onReceiveMethod of every activity (may be selected )just call finish(). In this your activities will be removed from the stack. Further you can visit this for more help: On logout, clear Activity history stack, preventing "back" button from opening logged-in-only Activites

Lustrum answered 25/5, 2012 at 5:57 Comment(3)
have you read my questions body? as I wrote - I'm using that solution for now. can you tell me if from some reason using this solution won't cause android firmware engineers to raise an eye brown about using broadcast receivers this way?Mezoff
sory about that. i didn't read ur question properly. In my knowledge there is no such way. But most probably m wrong. One thing is if you want an activity not to reside in stack just start it with no history property, can be set through code and xml both.Lustrum
thanks for your answer, I need my activity to stay in stack at some points, so using the no history property is not an option for me. if you'd like to hear, I can be more specific about the situation that made me need this requierment: I need to handle c2dm push notification from inside and outside the application - open specific screens acording to it, and when jumping to some activity - clear all activities beside the "MainMenuActivity" from stackMezoff
M
1

I also had the same problem. What I did is, I kept a static array list, and whenever I used to go from one activity to another, in the onCreate() method of new activity, I added the object of current activity into that list like this:

SomeClass.addActivity(CurrentActivity.this);

I added the above statement in each activity.

The addActivity():

public void addActivity(final Activity activity) {
            activityList.add(activity);
        }

And when I wanted to clear the stack, I called:

public boolean clearStack() {
        for (Activity activity : activityList) {
            activity.finish();
        }
        activityList.clear();
        return (activityList.isEmpty());
    }

In this way, I cleared my activity stack.

Thank you :)

Malleolus answered 24/5, 2012 at 11:40 Comment(1)
your solution can work, but it's not the right way to do that. It's not ok to hold references to activities... It can cause easily to memory leaks. anyway thanks for trying to helpMezoff
M
0
    Intent myintent = new Intent(YourCurrentActivity.this, YourNextActivitys.class);
    myintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(myintent);
    finish();

I think this is the right way to clear all previous activities...

Mausoleum answered 25/5, 2012 at 5:44 Comment(1)
according to : developer.android.com/reference/android/content/… doing so will clause in the way not only "YourNextActivitys" class, but also all activities between him to his other instance in the stackMezoff

© 2022 - 2024 — McMap. All rights reserved.