brian
brian

Reputation: 6922

How to setFlag of intent on Android

I use AActivity call BActivity, and BActivity call CActivity.
As below code:
In AActivity:

Intent intent = new Intent();
intent.setClass(AActivity.this, BActivity.class);
startActivity(intent);

In BActivity:

Intent intent = new Intent();
intent.setClass(BActivity.this, CActivity.class);
startActivity(intent);

If in CActivity I pressed back button, I want to launch AActiviy directly.
But not call BActivity.
How can I modify it?

Upvotes: 0

Views: 354

Answers (2)

EJK
EJK

Reputation: 12524

In BActivity:

Intent intent = new Intent();
intent.setClass(BActivity.this, CActivity.class);
this.finish();  //***** Add this
startActivity(intent);

This will pop B off the back stack, so that when you press the back button from CActivity, the next one in the stack, AActivity will be displayed.

Note - This differs from the answer above. In this approach, you return to AActivity in its previous state (for the most part). In the first answer above, you would be launching a new instance of AActivity thus adding one more activity to the back stack. You need to pick which approach suites your needs better.

Upvotes: 1

wtsang02
wtsang02

Reputation: 18873

You can override the back button in your CActivity to

@Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
           Intent intent = new Intent();
           intent.setClass(CActivity.this, AActivity.class);
           startActivity(intent);
        }
        return true;
    }

Upvotes: 1

Related Questions