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 activ开发者_如何转开发ity stack how to do this.
Please give me a example.
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.
use onBackPressed()
and intent mechanism to launch or reshow necessary activity.
//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);
}
精彩评论