Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6805069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:32+00:00 2026-05-26T19:32:32+00:00

Th question: Can I re-use RadioButton objects over and over again in an child

  • 0

Th question: Can I re-use RadioButton objects over and over again in an child activity?

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.

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);
    } ;
    <....more subclasses....>
    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();
    }
}

In the parent activity i have:

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

In the child activity i have this:

    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);

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:

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

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.

Ideas:

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

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:32:33+00:00Added an answer on May 26, 2026 at 7:32 pm

    make sure you remove all the radiobuttons from the all radiogroups. Basically your right the radiobuttons are saving pointers to the non-existant raidogroups and no there isn’t isn’t a way to reassigned without calling removeAllViews on all the radiogroups. The best place to do that will be the onDestroy if your sure thats being called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this question can create a misunderstanding: I know I have to use CSS to
pretty simple question: can I use NBuilder to create a collection of x number
I have a short and simple question: Can I use NSSpeechSynthesizer or Mac OS's
Brief question What command can I use to make my DataSet refresh it's connection
This is a silly question, but you can use this code to check if
HTML Javascript question to get the selected value of a input-select I can use
There are similar question, but not regarding C# libraries I can use in my
On this question , there's an answer that states: You can use typedef to
Well, question is in title. Is it possible at all? I can use css
This question was originary in my head as Can I use AWT controls in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.