美文网首页
Activity的recreate()方法,你用过吗?

Activity的recreate()方法,你用过吗?

作者: 三月三儿三 | 来源:发表于2020-05-23 21:26 被阅读0次

    要不是看别人的项目,都还不知道这个东西。

    在我看的项目中这个方法应该是用在切换主题的时候调用重建界面的,查看该方法的调用会发现在AppCompatActivity中用于控制的代理类的实现AppCompatDelegateImpl中有一个方法updateForNightMode调用了recreate(),由此可见该方法的作用。
    先看官方的注释

    /**
    * Cause this Activity to be recreated with a new instance.  This results
    * in essentially the same flow as when the Activity is created due to
    * a configuration change -- the current instance will go through its
    * lifecycle to {@link #onDestroy} and a new instance then created after it.
    */
    

    大致就是会先销毁当前实例,然后创建新的实例,生命周期基本一致。
    调用该方法会额外调用onSaveInstanceStateonRestoreInstanceState这两个方法。这两个方法就很熟悉了,一般在系统资源不足activity被杀死然后重建就会涉及这两个方法。
    其实这个方法就可以当作这种形式进行处理。

    相关文章

      网友评论

          本文标题:Activity的recreate()方法,你用过吗?

          本文链接:https://www.haomeiwen.com/subject/svzhahtx.html