在activity中调用 moveTaskToBack (boolean nonRoot)方法便可将activity 退到后台,注意不是finish()退出。android
参数为false表明只有当前activity是task根,指应用启动的第一个activity时,才有效;this
若是为true则忽略这个限制,任何activity均可以有效。spa
moveTaskToBack调用后,task中activity的顺序不会发生变化,例如A启动B,B中调用此方法退到后台,从新启动应用会调用B中的onRestart-onStart-onResume方法,不会从新调用onCreate,并且在B中按下back键返回的仍是A,这就是退到后台的功能。orm
另外在activity中按下back键,实际是调用了finish方法,应用退出。虽然应用已经退出,但进程没有被杀死,android中一个应用运行于独立的一个虚拟机实例中,因此在从新启动应用时一个类中的静态对象还保持着运行时的状态,注意在合适位置复位这些状态。对象
关于moveTaskToBack 方法,官方API解释以下:进程
Since: API Level 1虚拟机
Move the task containing this activity to the back of the activity stack. The activity's order within the task is unchanged.it
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.table