VK.Dev
VK.Dev

Reputation: 811

How to show activity from activity stack

i need some help about activity stack.

In my app i have 6 screens user navigate from

  1-->2-->3-->4-->

when i go to 4 screen i have cancel button

when user click on that button it should go to second screen and

when user clicks back button on keypad it should go to 1 screen which was already in activity stack how to do this.

Please give me a example.

Upvotes: 2

Views: 932

Answers (3)

pawelzieba
pawelzieba

Reputation: 16082

Just use FLAG_ACTIVITY_CLEAR_TOP

When user clicks button:

Intent intent = new Intent(ActivityD.this, ActivityB.class);
Intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

When user presses back:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Intent intent = new Intent(ActivityD.this, ActivityA.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }
    return super.onKeyDown(keyCode, event);
}

This code will take the user to first activity.The activity won't be recreated because of FLAG_ACTIVITY_SINGLE_TOP. When activity is already on back stack the onNewIntent() will be invoked in which you can use data from intent for example.

If you want such behaviors as default for yours activities put these flags to android manifest into activity declarations.

Upvotes: 3

Jana
Jana

Reputation: 2920

//For Back btn on screen 4

Onclick(View v)
{
if(v==Backbtn)
{
finish();
startActivity(new intent(this,SecondActivity.class));
}
}

// for back key press to return to 1 screen

public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            startActivity(new intent(this,FirstActivity.class));
                     return true;
        }
        return super.onKeyDown(keyCode, event);
    }

Upvotes: 0

Vladimir Ivanov
Vladimir Ivanov

Reputation: 43098

use onBackPressed() and intent mechanism to launch or reshow necessary activity.

Upvotes: 0

Related Questions