Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove activities manually from Android app stack

I been working on Android Native App , What i was trying to do is :

Activities - A -> B -> C  Then  A-> B -> C -> C  . 

From C Activity if it again point to C then i want to remove C , B from stack manually . On my back it should move only to A .

I tried finish() but problem is :

Activities - A -> B ->  C  Then  A-> B -> C -> C  on finish A -> B -> C  required state A-> C .

Is anyone know how to catch all activities in stack and remove specific activities from stack ??

like image 795
Rizvan Avatar asked Nov 06 '13 07:11

Rizvan


1 Answers

In Activity C, override onBackPressed and add in something like:

@Override
public void onBackPressed() {
    if (shouldGoBackToA) {  // There are various ways this could be set
        Intent intent = new Intent(this, AActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    } else {
        finish();
    }
}

FLAG_ACTIVITY_CLEAR_TOP will cause it to go down the stack to the existing copy of A Activity instead of starting a new one. From the docs:

public static final int FLAG_ACTIVITY_CLEAR_TOP
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent.

like image 116
blahdiblah Avatar answered Sep 21 '22 05:09

blahdiblah