Android 解决方案 笔记 1
01,程序置入后台,不退出,就像微信和QQ那样
(效果和按下HOME键差不多)
像微信
那样,按返回按钮以后,不退出程序,而是置入后台运行。也就是说,主Activity不会触发onDestroy()
。
有一个方法叫onBackPressed()
里面定义了用户按下返回按钮的动作。大部分时候,就是对当前Activity执行了finish()
,也就是销毁当前Activity
要么返回上一级,要么退出程序,当前Activity执行onDestroy()
。
这个动作是在onBackPressed()
里面定义的。
可以覆盖这个方法,然后取消自动添加的super.onBackPressed()
,这样就不会执行默认的动作,转执行我们需要的动作。
(如果要求不执行任何动作,也就是返回
按钮没有响应,就空着)
有一个方法叫moveTaskToBack (boolean nonRoot)
public boolean moveTaskToBack (boolean nonRoot)
Added in 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.Parameters
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.
Returns
If the task was moved (or it was already at the back) true is returned, else false.
大概意思是说,可以让当前Activity移到后台,保持Activity顺序。下次点击图标进入的时候,Activity顺序依然有效。
如果让任意Activity都有效,参数就用true<