Android Activity
I have 5 activities and the flow is like开发者_如何学C this
1 → 2 → 3 → 4 → 5
at the 5th activity, upon pressing the back button, is it possible to get back to activity 2 or 3 wihtout finishing any activity? Currently I only get to the 4th one.
There's something like ActivityHistory
. I am not very sure about the exact keyword, but there does exist something like that. You can traverse through it.
Maybe this link helps!
If u want to get 2 or 3 u want to write code on back Key
@Override
public boolean onKeyDown(int i, KeyEvent event) {
if (i == KeyEvent.KEYCODE_BACK) {
Intent i=new Intent(getbaseapplicationcontext(),activity2.class)
startActivity(i)
return true;
}
return super.onKeyDown(i, event);
}
And another way is
u want to finish Activity whatever u doesnot want to As u need here Activity4
One solution to your problem can be, simply override the Back hard button, and then start whichever activity you want to start. But overriding the default behavior is not recommended. Revert back for any query.
When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes. On Back Button Pressed.
@Override
public boolean onKeyDown(int i, KeyEvent event) {
if (i == KeyEvent.KEYCODE_BACK) {
Intent intent = new Intent( YourActivity.this, New Activity.class );
intent.setFlags( Intent.FLAG_ACTIVITY_CLEAR_TOP );
startActivity( intent );
return true;
}
return super.onKeyDown(i, event);
}
精彩评论