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.
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.