Activity save and restore status of Android learning summary

Start an activity in Android. If you click the home button, the activity will not be destroyed, but some data will be lost during some operations, as follows:

Java class:

XML layout file:

Here, a double variable is defined in the program. When we click the read button, the value of the variable will be added and displayed. Open the program and operate. Everything is normal:

After we click on the settings, we turn the screen horizontally. Hey, where's Pai?

In fact, when we switch between horizontal and vertical screens, the activity has been destroyed due to the system mechanism. But why does this page still exist? This is because the destruction is not the user's initiative to exit, so the Android system realizes the function of saving these States, but some data can not meet the requirements of retention and are not retained, just like the poor pai in the program is abandoned!

What should we do to preserve data during such horizontal and vertical screen switching? Here we need to override onsaveinstancestate() method:

After saving, how can we take it out? Very simply, you only need to judge whether its parameter is null in the oncreate () method, and take it out if it is not null.

Now we can see that the PAI has not been destroyed after the screen switch.

The above is the activity saving and recovery status introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>