user1910721
user1910721

Reputation: 41

Close all child activities down to parent activity

If i have the following

Parent activity > activity 1 > activity 2 > activity 3

Pressing the back button will go back to: Parent activity > activity 1 > activity 2

Pressing again will go to: Parent activity > activity 1

is there any way that I can programatically finish activity 1,2 and 3 from activity 3 itself.

In IOS there is a function called popToRootViewControllerAnimated, Which is the type of concept I require in Android

thanks

Upvotes: 2

Views: 1533

Answers (4)

Ozan.
Ozan.

Reputation: 72

maybe this algorithm can be usefull;

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    switch(keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //here you'll check if activity3 alive then join
            //else if activity2 alive then join
            //else if activity1 alive then join
            //else finish(); 
        }
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 0

Vasudev
Vasudev

Reputation: 1996

Check in onResume() of activity2 if you came from activity3 and call finish(), if yes. Same for activity1.

Upvotes: 0

Passionate Androiden
Passionate Androiden

Reputation: 732

Add Flag Intent.FLAG_ACTIVITY_CLEAR_TOP while you call startActivity from your last activity. It will clear the activity stack

Upvotes: 1

Sam
Sam

Reputation: 86948

Launch the child Activities with startActivityForResult() and then in onActivityResult() call finish(). This will bring you back to the Parent Activity.

Alternatively you can also use the Intent flag FLAG_ACTIVITY_CLEAR_TOP while calling startActivity() on Parent Activity from Activity 3.

Upvotes: 1

Related Questions