关于android状态保存 onSaveInstanceState

      写了一年的安卓程序,第一次写博客,如是你看到勿喷。不是大神,只是热爱。

     关于安卓的状态保存的意义不累赘,

     只分享状态的保存的 使用的一些细节注意事项:


onSaveInstanceState的执行时间执行条件

             1,执行条件,

     * Called to retrieve per-instance state from an activity before being killed
     * so that the state can be restored in {@link #onCreate} or
     * {@link #onRestoreInstanceState} (the {@link Bundle} populated by this   method
     * will be passed to both).

       保存状态在被杀死之前,为了这个状态被回复在onCreate和onRestoreInstanceState的方法中,

       onRestoreInstanceState方法在首次onCreate事并不会被执行,只有 运行时 内存不足 杀死activity后 从新回到 activity的时候 才会被执行 该方法


     *
     * <p>This method is called before an activity may be killed so that when it
     * comes back some time in the future it can restore its state.  For example,
     * if activity B is launched in front of activity A, and at some point activity
     * A is killed to reclaim resources, activity A will have a chance to save the
     * current state of its user interface via this method so that when the user
     * returns to activity A, the state of the user interface can be restored
     * via {@link #onCreate} or {@link #onRestoreInstanceState}.

    

     *
     * <p>Do not confuse this method with activity lifecycle callbacks such as
     * {@link #onPause}, which is always called when an activity is being placed
     * in the background or on its way to destruction, or {@link #onStop} which
     * is called before destruction.  One example of when {@link #onPause} and
     * {@link #onStop} is called and not this method is when a user navigates back
     * from activity B to activity A: there is no need to call {@link #onSaveInstanceState}
     * on B because that particular instance will never be restored, so the
     * system avoids calling it.  An example when {@link #onPause} is called and
     * not {@link #onSaveInstanceState} is when activity B is launched in front of activity A:
     * the system may avoid calling {@link #onSaveInstanceState} on activity A if it isn't
     * killed during the lifetime of B since the state of the user interface of
     * A will stay intact.


     大致意思是 不要认为这个方法一定会在onPause和onStop之间调用,如过是你自己主动终止结束这个activity将不会执行这个方法,因为你确定要关闭finish不需要保存任何状 态;

    


   总结来说,这个方法只会在activity将要进入 安卓堆栈第二层的时候 执行 ,目的是当系统资源不足,运行时 回收内存,杀死activty时保存状态,当activity被回退执行onCreate和onRestoreInstanceState时回复之前的状态;


    2,执行时间:

      安卓高级开发一书上说是在onpause之前执行,话说尽信书 不如无书,实际运行发现,是在onpause和onstop之前执行,而且在onpause执行完大概有1s的延迟时间,

可以安全的认为该方法在进程终止之前一定被调用。

     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值