android中实现应用程序的Activity按返回键时,不销毁该Activity,在后台继续运行
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(false);
return true;
}
return super.onKeyDown(keyCode, event);
}
其中主要起作用的代码是:moveTaskToBack(false);这一行代码;
其API文档描述如下 :
Move the task containing this activity to the back of the activity stack. The activity’s order within the task is unchanged.
参数:
nonRoot If false then this only works if the activity is the root of a task; if true it will work for any activity in a task.
返回:
If the task was moved (or it was already at the back) true is returned, else false.