recreate()和startActivity(getInte
2019-04-16 本文已影响12人
五月天外飞仙
在Activity里的各个生命周期里打上Log方法。
调用
startActivity(getIntent());
finish();
方法后,打印出来的生命周期:

调用
recreate();
方法后,打印出来的生命周期:

可以看到recreate()方法比startActivity(getIntent())多调用了一个onRestoreInstanceState方法,这个onRestoreInstanceState方法就是Activity用来自动保存状态的方法,主要是保存View的状态。大致是遍历View树,并调用每个View的SaveInstance方法。
所以如果要保存当前的View状态,就使用recreate()方法。
如果想完全重启Activity,就调用startActivity(getIntent())方法。