Re-using a radiobutton in Android
<p>Th question: Can I re-use RadioButton objects over and over again in an child activity?</p>

<p>I have a parent activity and a child activity.  In the child activity, I have a large number of radio button displayed in a UI.  In order to provide databinding from the parent down to the child, I have created a class (below) which contains a collection of RadioButtons.  To populate the child activity, I pass a reference to this class down to the child which then groups the radioButtons into RadioGroups and displays them.  I do this because the checked status of each button is now automatically available in the parent class, without the need to transfer any data through bundles.</p>

<pre><code>public class GeneralAttribute{
    Activity  mThis;
    public class Gender {         // Mutually exclusive members
        String      categoryDesc  = "Gender of user";
        RadioButton isUnspecified = initRadioButton("Unspecified", true);
        RadioButton isMale        = initRadioButton("Male"       , false);
        RadioButton isFemale      = initRadioButton("Female"     , false);
    } ;
    &lt;....more subclasses....&gt;
    RadioButton initRadioButton(String str, Boolean b) {   // Factory
        float cLayoutWeight = 0.5f;
        RadioButton rb = new RadioButton(mThis);
        rb.setText   (str);
        rb.setChecked(b);
        rb.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, cLayoutWeight));
        return rb;
    }
    GeneralAttribute(Activity localThis){ // Constructor
        mThis = localThis;
        gender     = new Gender();
        handedness = new Handedness();
        location   = new Location();
    }
}
</code></pre>

<p>In the parent activity i have:</p>

<pre><code>public class Parent(...)

   public GeneralAttribute mGeneralAttribute;              // Member class of RadioButtons
   public static SPIGestureLoggerActivity TopLevelActivity;// Reference to the parent activity

   public void onCreate(Bundle savedInstanceState) {
       TopLevelActivity = this;                            // Assign this to the reference
       mGeneralAttribute = new GeneralAttribute(this);     // Initialize the class of RBs
       startActivity(child);                               // Start the child
</code></pre>

<p>In the child activity i have this:</p>

<pre><code>    radiogroup = new RadioGroup(this);
    radiogroup.setOrientation(RadioGroup.VERTICAL);
    radiogroup.addView(Parent.TopLevelActivity.mGeneralAttribute.gender.isUnspecified);
    radiogroup.addView(Parent.TopLevelActivity.mGeneralAttribute.gender.isMale);
    radiogroup.addView(Parent.TopLevelActivity.mGeneralAttribute.gender.isFemale);
    Parent.TopLevelActivity.mGeneralAttribute.gender.isUnspecified.setChecked(true);
    mLinearLayout.addView(radiogroup);
</code></pre>

<p>This works fine....the first time the child activity is displayed.  The second time it is displayed I get an exception.
In summary, here is the chain of events:</p>

<ul>
<li>create the class of RadioButtons,</li>
<li>pass them to the child,</li>
<li>add them to a new RadioGroup</li>
<li>collect user choices</li>
<li>finish the child acitivty (which should destroy the RadioGroups)</li>
<li>use the data in the parent,</li>
<li>start the child activity again,</li>
<li>attempt to add the RadioButtons to new RadioGroups...
...Exception.</li>
</ul>

<p>I can avoid this problem, if I null the class and reconstruct it.  However, I would like to re-show the choices made from the first viewing with the second viewing.</p>

<p>Ideas:</p>

<ul>
<li>Are the radioButtons saving pointers to the non-existant RadioGroups from the first viewing?</li>
<li>Is there a way to re-assign the view parent on each radio button in the class?</li>
</ul>

<p>P.S. You may ask why I'm not using XML.  For one, I will have 100+ of these radio buttons and I think it will be too painful to manage through XML.  For another, I just like working programmatically on these things.</p>
