Android back problem
I have three activities, from A it is going to B, from B it is going to C. I am u开发者_StackOverflow中文版sing following code to transfer from one activity to another.
Intent intent = new Intent().setClass(this, B.class);
startActivity(intent);
I want that when I use the back button, it should come to B if it is at C(which is ok for me), but if I use back button at B activity, it should not go to A, it should directly go out the application. How it can be arranged?
There you go
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
call finish(); when you launch the activity B from the activiy A
Override the member function onBackPressed()
inside your Activity
class.
Example:
public void onBackPressed() {
Intent intent = new Intent().setClass(this, B.class);
startActivity(intent);
}
In class A you would put:
Intent intent = new Intent(this, B.class);
startActivity(intent);
finish();
This will remove class A from the Activity stack.
精彩评论