Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Views with same ids getting the same attrs when restored

Tags:

android

I have a custom component with one RadioGroup that has two RadioButton, ids @+id/radioButton1 and @+id/radioButton2. On the other hand, there is a TabActivity with five tabs. On each tab, this component is used more than one time.

Here's the problem, when orientation changes and the activity is recreated, all of the RadioButton is loaded with the same attrs, this includes android:text, android:margin and even the styled attrs that i've created. It also happens with all the CheckBox that has the same ids.

I spent some time trying to discover why this was happening and concluded that android is doing that in onRestoreInstanceState. If I comment the line that calls the super method it works fine.

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    // super.onRestoreInstanceState(savedInstanceState);

}

Is this how it's supposed to be? Or what could one do to cause this?

I'm guessing that it's not a normal behaviour because when a custom view is created, it inflates the same layout with the same views that has the same ids. So it's not possible to instantiate a new custom view generating different ids for its child every time. Using the code above or android:configChanges on the manifest seems to be a bad workaround to me. So, any help is appreciated.

like image 670
João Melo Avatar asked Oct 18 '12 16:10

João Melo


People also ask

What is custom view android?

Custom Views is just a way to make an android developer a painter. When you need to create some custom and reuse the views when it is not provided by the Android Ecosystem. Custom Views can be used as widgets like TextView, EditText etc.

How do I create a custom view?

Create a custom viewGo to View > Workbook Views > Custom Views > Add. In the Name box, type a name for the view. Tip: To make a view easier to identify, you can include the name of the active worksheet in the name of a view. Under Include in view, select the check boxes of the settings that you want to include.

What are the types of custom views?

In Android, there are actually two other Views readily available to do this: Spinner and AutoCompleteTextView , but regardless, the concept of a Combo Box makes an easy-to-understand example. To create a compound component: The usual starting point is a Layout of some kind, so create a class that extends a Layout.


2 Answers

After some time searching for an explanation, I finally found a great one from Romain Guy in the android google group, although it doesn't mention custom views.

"It's ok to use same ids except when you need the views to save their state. The id is what identifies views when their state is saved so the toolkit simply think that your two views are the same. But that's true within an activity only. "

"There is no problem using the same id in two different activities. And there is no problem using the same id inside the same activity if you're careful about what you're doing (for instance, if the views with the shared ids do not need to save any state.) In your case, because the activities are part of a set of tabs, they are actually tied to a unique context which means you will run into problems with shared ids. "

Here's the link.

like image 94
João Melo Avatar answered Oct 19 '22 07:10

João Melo


What does your savedInstanceState method look like?

I would suggest that the variables assigned to the radiobutton texts are not persisting between orientation changes. Remember, the activity is completely rebuild/restarted when orientation changes.

like image 25
Music Monkey Avatar answered Oct 19 '22 06:10

Music Monkey