开发者

Android custom component doesn't retain value on orientation change

开发者 https://www.devze.com 2023-03-14 21:22 出处:网络
I have a layout th开发者_如何学运维at uses TextView and a custom control I created that combine a TextView and two buttons for an integer up/down control. The TextView retains its value when the devic

I have a layout th开发者_如何学运维at uses TextView and a custom control I created that combine a TextView and two buttons for an integer up/down control. The TextView retains its value when the device is rotated, I'm calling the save/restore instance state. I'm not capturing any values as the device retains the values. However, this is not the case for my custom component.

Is there something I need to implement in a custom control so when the device orientation changes it retains the values like the native controls do?

Thank you.


Throw android:configChanges="orientation" into your activity tags in your AndroidManifest.xml file.


You can prevent your activity from being destroyed and recreated as Haphazard suggests, and this is probably the best solution.

If you don't want to do that, then you need override the following three methods:

  • isSaveEnabled() (override to return true)
  • onSaveInstanceState()
  • onRestoreInstanceState(Parcelable)

Consult the documentation (such as it is) for how to do this. There's also an example of how it's done here. The view will need a non-default ID to have it's onSaveInstanceState() called or else you'll also need to override onSaveInstanceState(Bundle) in your activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号