Android 返回键 返回上一级Activity
功能需求:有两个Activity,一个为MainActivity,一个为ChildActivity,
在MainActivity中单击button进入ChildActivity,
在ChildActivity中单击返回键返回MainActivity
MainActivity代码:
view plaincopy to clipboardprint?
button1.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent intent=new Intent();
intent.setClass(MainActivity.this, ChildActivity.class);
startActivity(intent);
//MainActivity.this.finish(); //MainActivity不可以finish
}
});
ChildActivity代码:
view plaincopy to clipboardprint?
public boolean OnKeyDown(int keyCode,KeyEvent event){
if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
Intent intent=new Intent();
intent.setClass(ChildActivity.this, MainActivity.class);
startActivity(intent);
ChildActivity.this.finish();
}
return false;
}