13

How to finish any previous activity in application stack (at any level , I mean not immediate parent) , from current activity like on some particular event I want to invalidate this previous activity? Any help ? Thanks.

Pritam
  • 2,367
  • 5
  • 34
  • 50

4 Answers4

33

I know this answer may be late, but I'm still going to post it in case someone is looking for something like this.

What I did is I declared a static handler in in ACTIVITY_A

public static Handler h;

and in my onCreate() method for ACTIVITY_A, I have

h = new Handler() {

        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch(msg.what) {

            case 0:
                ACTIVITY_A = null;
                finish();
                break;

            }
        }
        
    };

Now, from any activity after this one, such as ACTIVITY_B, or ACTIVITY_C I can call

ACTIVITY_A.h.sendEmptyMessage(0);

which then calls finish() in ACTIVITY_A and ta-da! ACTIVITY_A is finished from a different activity.

oguzhan
  • 2,073
  • 1
  • 26
  • 23
Ryan
  • 882
  • 10
  • 11
  • 2
    Note that in this case static handler object will hold reference to Activity_A. You can leak whole Activity_A. – Robert Jan 16 '13 at 13:47
  • 2
    but you can't do that. finish() is non-static. a static function can't call a non-static function. I am confused. – dariusiv Aug 07 '16 at 15:39
6

So I tired this, but didn't work after I did more deeper testing (I leave it here for future reference): android:clearTaskOnLaunch

Suppose, for example, that someone launches activity P from the home screen, and from there goes to activity Q. The user next presses Home, and then returns to activity P. Normally, the user would see activity Q, since that is what they were last doing in P's task. However, if P set this flag to "true", all of the activities on top of it (Q in this case) were removed when the user pressed Home and the task went to the background. So the user sees only P when returning to the task.

https://developer.android.com/guide/topics/manifest/activity-element.html

UPDATE This did work

Intent intent = new Intent(this, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
narancs
  • 5,234
  • 4
  • 41
  • 60
4

You can use the Intent flag FLAG_ACTIVITY_CLEAR_TOP to restart an activity from the stack and clear everything that was above it. This isn't quite what you're asking, but it might help.

To do this, use:

Intent intent = new Intent(context, classToBeStarted.class);
intent.setFlag(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Steve Haley
  • 55,374
  • 17
  • 77
  • 85
  • I'm having the same problem. First I start activities A, B and C. Then I start activity D with Intent.FLAG_ACTIVITY_CLEAR_TOP set. This should make D the only activity on the stack, right? Somehow this is not the case, because when I press the back button, I go back to one of the previous activities. – neu242 Apr 29 '10 at 11:04
  • 2
    No, that's not how CLEAR_TOP works. CLEAR_TOP removes anything that is 'above' the target activity in the stack. So if you have A, B, C and then you start A again with CLEAR_TOP, then B and C get killed. If you started D or restarted C, nothing would happen. That's why I said that it isn't quite what he was asking for, but it can be useful. For example, if you have a main menu screen that will be the root activity, you can relaunch it with CLEAR_TOP whenever the user returns to it to kill everything else. – Steve Haley Apr 29 '10 at 11:15
  • So to achieve what I wanted, I might do something like this instead: 1) Start A, B, C. 2) Start A with CLEAR_TOP and some data identifying D. 3) In A, call finish() and start D. Feels like a dirty hack, but should work..? – neu242 Apr 29 '10 at 11:33
  • Actually I do not want to clear the whole stack. neu242 let me know if your way works ..but that would make user get moved from current focused activity, which I want to avoid. I have asked same question on http://www.coderanch.com/t/493238/Android/Mobile/Finish-any-previous-activity-stack gave some other possible ways. – Pritam Apr 29 '10 at 12:20
2

This may be possible by using static variables. Like use a boolean variable activity_name_dirty = false; mark this as true as soon as your condition of invalidating that particular activity occurs. So any time later when calling this activity have a check on the state of activity_name_dirty. You may then use Activity Flags to create a new instant as described in Activity Fundamentals

Pritam
  • 2,367
  • 5
  • 34
  • 50