我在不同手机上测试了我的应用。有一件奇怪的事情。在所有手机中,当我使用主页按钮将应用程序发送到后台并返回时,onCreate()
永远不会运行。但在Nexus中,onCreate()
方法运行。我重新创建了我想要避免的全部视图。我不知道如何在savedInstanceState
中保存视图和其他信息。如何避免重新运行onCreate()
?任何帮助将不胜感激。
编辑:即使在活动生命周期中,onResume()
也只能在从后台返回活动后执行。
答案 0 :(得分:1)
But in Nexus, the onCreate method runs. The whole of my view is recreated which I want to avoid.
You shouldn't make assumptions on whether onCreate()
will be called or no. Prepare your app for a situation, when system destroys app's process, which would initiate onCreate()
to be called upon opening app. This can be easily tested with "Do not keep activities" developer mode.
I don't know how to save the views and other info in savedInstanceState.
If you use framework views (e.g. Button
, TextView
) which have id
associated with them (e.g. android:id="@+id/some_id"
), system will take care of saving their state, you should not care about it. If you have custom components you should take care of implementing that logics yourself.
How to avoid rerunning onCreate?
If system performed onCreate
, that means it had good reason to do that. In other words, you cannot tell "please, do not call onCreate, jump to onResume right away".