Question
How to save the state of an instance of a view widget when, using XML-specific widget layouts, the components of individual widget instances have the same identifier?
Example
Take, for example, the NumberPicker widget that is used in the TimePicker TimePicker (note that NumberPicker does not appear in the SDK). This is a simple widget with three components that are pumped from number_picker.xml : one increment button, one decrease button and one EditText , where you can directly enter a number. In order for the code to interact with these widgets, they all have identifiers ( R.id.increment , R.id.decrement and R.id.timepicker_input respectively).
Let's say you have three NumberPicker in the XML layout, and you give them different identifiers (e.g. R.id.hour , R.id.minute ). ΒΉ This layout is then inflated to represent the contents of the activity. We decided to change the orientation of the activity, so Activity.onSaveInstanceState(Bundle) helps to keep our view state for each view with an identifier (this is the default behavior).
Unfortunately, the three NumberPicker have an EditText that all have the same identifier - R.id.timepicker_input . Thus, when activity is restored, the most remote in the hierarchy of representations is the one whose state seems to be preserved for all three of them. In addition, the focus moves to the first NumberPicker during recovery, regardless of which focus was saved.
TimePicker circumvents this problem by keeping the state itself separate. Unfortunately, this will not save the cursor position or focused image without additional work. I'm not sure how he saves this state, if he does at all (and fast playback with a time input dialog seems to indicate that it can somehow).
Please check out the sample code to demonstrate this problem: https://github.com/xxv/AndroidNumberPickerBug
ΒΉ In the view hierarchy, the LinearLayout identifier is LinearLayout , which NumberPicker applies to your identifiers.
android android-layout android-custom-view
Steve pomeroy
source share