Do I still need to restore Activities when I rotate my phone?

1

Not long ago, every time an Android application was developed, you had to worry about the data that was on the screen when you rotate the phone (change the orientation). As explained in this post you had to re-create activity.

Recently, when programming, I've noticed that when you rotate your cell phone (both on the AVD and on the real cell phone) the data is no longer destroyed. Anyone know if something has changed? Is it no longer necessary to manually restore the Activity?

    
asked by anonymous 19.07.2016 / 06:54

1 answer

0

It still needs to, yes. What happens is that many components already do this restore for you automatically and, therefore, you do not realize. A text field can, for example, save what was written on it and restore when the screen is re-created. Many components have a satisfactory implementation. However, when the state is complex and depends on the logic of its application, it has no way of doing miracles.

In addition, something that is possible now and was not before is the use of setRetainInstance(true) in Fragment s, so that they are not destroyed during a configuration change (in our case, screen rotation). However, this does not solve the problem of the loss of state in case your Fragment is destroyed by the system to recover memory, then the best thing to do is to save and restore the state yourself.

    
19.07.2016 / 14:22