Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finish any previous activity in stack from current activity?

Tags:

android

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.

like image 553
Pritam Avatar asked Apr 27 '10 14:04

Pritam


People also ask

How do you finish previous activity?

FLAG_ACTIVITY_CLEAR_TASK|Intent. FLAG_ACTIVITY_NEW_TASK . It will totally clears all previous activity(s) and start new activity.

What code would you use to go back to a previous activity?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

How can you start another activity from the current activity?

To start an activity, use the method startActivity(intent) . This method is defined on the Context object which Activity extends. The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo.


2 Answers

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:
                finish();
                break;

            }
        }

    };

Now, from any activity after this one, such asACTIVITY_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.

like image 97
Ryan Avatar answered Oct 03 '22 04:10

Ryan


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);
like image 22
narancs Avatar answered Oct 03 '22 03:10

narancs