I do my home task related to flags of intents in android and have created 10 activities each of which calls the next one subsequently (MainActivity opens activity2, activity2 calls activity3 and etc). 10th activity calls activity4 with flag CLEAR TOP, so in my guess, after 4th activity appears on screen , I shouldn't be able to return back to 10th one, but I do someway. Interestingly enough, when I navigate to the 5th activity from 4th (after i passed 10th) and navigate back, my stack gets cleared and i return to 3rd stack (but not to the 10th). How can I apply this behavior to the 4th activity itself without need to go to 5th one again?

public class MyActivity9 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main9);
    Button b=findViewById(R.id.btn_to_2);
    final Intent intent= new Intent(this,MyActivity10.class);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(intent);
        }
    });
}

}

public class MyActivity10 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main10);
    Button b=findViewById(R.id.btn_to_2);
    final Intent intent= new Intent(this,MyActivity4.class);
    //  CLEAR_TOP
    intent.addFlags(FLAG_ACTIVITY_CLEAR_TOP);
    //  CLEAR_TOP
    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(intent);
        }
    });
}

}

1 Answers

0
Jay P On

I think that after starting any activity you should use the finish() method to close the previous activity. Then I think that you wont have any problem starting another intent.

e.g

b.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        startActivity(intent);
        finish();
    }
});