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

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

在我看的项目中这个方法应该是用在切换主题的时候调用重建界面的,查看该方法的调用会发现在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被杀死然后重建就会涉及这两个方法。
其实这个方法就可以当作这种形式进行处理。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。