2

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.

VK.Dev
  • 801
  • 4
  • 11
  • 24
  • I've edited ma answer. FLAG_ACTIVITY_SINGLE_TOP will take you to first screen which was already in activity stack. – pawelzieba Mar 28 '11 at 09:52

3 Answers3

3

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.

pawelzieba
  • 16,082
  • 3
  • 46
  • 72
0

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

Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
0

//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);
    }
Jana
  • 2,890
  • 5
  • 35
  • 45
  • Your code will start SecondActivity on Third, and First on Fourth activity. Pressing back button will show respectively Third and Fourth activity on top. – pawelzieba Mar 28 '11 at 09:05