您的位置:首页 > 其它

activity中oncreate的参数的意义

2016-07-25 18:37 501 查看
一、一个参数:

protected void onCreate(Bundle savedInstanceState)


这个参数配合以下两个方法使用

protected void onSaveInstanceState(Bundle outState)


public void onRestoreInstanceState(Bundle savedInstanceState)


1.前一个在app切换到后台时调用,在onPause和onStop之间调用,调用finish结束activity时不会调用此方法(会调用onpause和onstop)

2.后一个只有系统结束了应用(而不是程序主动结束应用)然后再打开的时候才会调用,比如:

横竖屏切换
切换到后台,系统因为内存不足杀死进程,再次打开时

   而自己主动结束应用,比如调用了finish,或者切换到后台再切换到前台

在调用onRestoreInstanceState的情况下,在onSaveInstanceState中保存到Bundle的数据会同时传给onCreate和onRestoreInstanceState,这就是onCeate参数中savedInstanceState的意义,如果不是在onRestoreInstanceState会调用的场景里,savedInstanceState为null

二、两个参数

public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState)


对应

public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState)

public void onRestoreInstanceState(Bundle savedInstanceState, PersistableBundle persistentState)

会对保存的信息做持久化(重启后还能访问)

此组方法调用时机不确定,一直没有模拟出来
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: