spykeburn
spykeburn

Reputation: 163

Android Activity back navigation recursive loop issue

I have three activities A,B,C. A being the parent activity. The navigation is supposed to be A---> B ---> C --->B----C---> B (Its a loop for B and C). The issue i'm facing is in the back navigation, which is supposed to be as shown in the image. Back from B should always go back to parent A and back from C should always go back to B. But sometimes B goes back to C which is resulting in a recursive loop between B and C. Can anyone help me out? Here is what I have tried:

ACTIVITY A
Intent intent = new Intent(A.this,B.class);
intent.putExtra("A",true);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);



ACTIVITY B
Intent intent = new Intent(B.this, C.class);
intent.putExtra("A",true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

OnBackpressed B
Intent intent = new Intent(B.this, A.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();


ACTIVITY C
Intent intent = new Intent(C.this, B.class);
intent.putExtra("A",A);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();


OnBackpressed C

if(A){
Intent iu= new Intent(C.this,B.class);
iu.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
iu.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(iu);
finish();
}

enter image description here

Upvotes: 1

Views: 536

Answers (3)

Ruchira Randana
Ruchira Randana

Reputation: 4179

You're creating several instances of Activity C and Activity B. That should not happen. There should be only one instance of each activity. You need to call finish() methods appropriately. Instead of calling startActivity(), you can call startActivityForResult() and get this done easily.

Upvotes: 1

Dharmendra Pratap
Dharmendra Pratap

Reputation: 527

1-Go from A to B without using finish()

2-Go from B to c without using finish()

3-Go from c to B using finish() in 
@Override
    public void onBackPressed() {
       finish();
    }

 4-Go from B to A using finish() in 
    @Override
        public void onBackPressed() {
           finish();
        }

Upvotes: 1

Jeffalee
Jeffalee

Reputation: 1085

What you could do is override the onBackPressed functionality for your activities, something like this:

for Activity B:

    @Override
    public void onBackPressed() {
        //start activity A
    }

for Activity C:

    @Override
    public void onBackPressed() {
        //start activity B
    }

Upvotes: 0

Related Questions