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 5928275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T14:16:24+00:00 2026-05-22T14:16:24+00:00

Edit: See the accepted answer. Lesson: Sometimes views will save and restore their state

  • 0

Edit: See the accepted answer. Lesson: Sometimes views will save and restore their state automatically. This happens AFTER onCreate. This can cause the overwriting of stuff you did in onCreate. If you don’t have unique ids, all views of a certain kind (in my case textboxes) can be overwritten with the same saved state. (ps: thanks for your help everyone!)

So, I have a simple linear layout and I want to add some views that have checkboxes with images. Everything works fine until I switch the orientation of my android phone. When I do it goes back through the onCreate but this time the checkboxes all end up with the same text. Weirdly, the images appear fine.

My question is: why is it doing this and how can I make it appear like the first time everytime?

In case that makes no sense here’s an example: (Edit: It turns out it always shows the last element’s text)

What I see at first

[] a *a's image*
[] b *b's image*
[] c *c's image*
[] d *d's image*

Then, after rotating my phone, it redraws

[] d *a's image*
[] d *b's image*
[] d *c's image*
[] d *d's image*

My original code is pretty complex, but i constructed the following that demonstrates the problem.

Main.java:

public class Main extends Activity {

ArrayList<AnswerView> answers = new ArrayList<AnswerView>();

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    TextView title = (TextView)findViewById(R.id.questionText);
    title.setText("This is a test");
    HashMap<String, Drawable> answerInfo = new HashMap<String, Drawable>();
    Resources res = getResources();
    answerInfo.put("a", res.getDrawable(R.drawable.flower_orange));
    answerInfo.put("b", res.getDrawable(R.drawable.flower_white));
    answerInfo.put("c", res.getDrawable(R.drawable.leaf));
    answerInfo.put("d", res.getDrawable(R.drawable.flower_yellow));
    setBoxes(answerInfo);
}

private void setBoxes(HashMap<String, Drawable> answerInfo) {
    LinearLayout answerList = (LinearLayout)findViewById(R.id.answerlist);
    AnswerView cb = null;

    //Remove all existing answer views
    answerList.removeAllViews();
    answers.clear();

    //For each possible answer create a answer views
    for (String s : answerInfo.keySet()) {
        cb = new AnswerView(this, s, answerInfo.get(s));
        answers.add(cb);
        String text = cb.getText();
        answerList.addView(cb);
    }
}
}

AnswerView.java

 public class AnswerView extends RelativeLayout  {

private CheckBox m_checkbox;
private ImageView m_image;
//private Context m_context;

public AnswerView(Context context, String answer, Drawable d) {
    super(context);
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.image_checkbox, this, true);
    m_checkbox = (CheckBox) view.findViewById(R.id.image_checkbox_cb);
    m_image = (ImageView) view.findViewById(R.id.image_checkbox_img);
    //m_context = context;
    m_checkbox.setText(answer);
    m_image.setImageDrawable(d);
    m_image.setVisibility(VISIBLE);

}

public void setChecked(boolean checked) {
    m_checkbox.setChecked(checked);
}

public boolean isChecked() {
    return m_checkbox.isChecked();
}

public String getText() {
    return m_checkbox.getText().toString();
}

}

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
        android:layout_height="wrap_content">
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="vertical" 
  android:padding="5dip">

  <TextView android:orientation="vertical" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/questionText"
    android:textSize="18sp"/>

  <LinearLayout android:orientation="vertical" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/answerlist"/>
  <LinearLayout android:orientation="horizontal" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center">
    <Button  
    android:layout_width="200dip" 
    android:layout_height="wrap_content" 
    android:text="Enter"
    android:id="@+id/buttonAnswerEnter"/>
    />
    </LinearLayout>

</LinearLayout>
</ScrollView>

image_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:orientation="horizontal">
  <CheckBox
   android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/image_checkbox_cb"></CheckBox>
  <ImageView
  android:id="@+id/image_checkbox_img"
  android:layout_width="100dip" 
  android:layout_height="100dip" 
  android:visibility="gone"></ImageView>
    </LinearLayout>
  • 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-22T14:16:25+00:00Added an answer on May 22, 2026 at 2:16 pm

    Does setting a unique ID for each AnswerView solve your issue? You could achieve that as follows:

    private void setBoxes(HashMap<String, Drawable> answerInfo) {
        LinearLayout answerList = (LinearLayout)findViewById(R.id.answerlist);
        AnswerView cb = null;
    
        //Remove all existing answer views
        answerList.removeAllViews();
        answers.clear();
    
        //For each possible answer create a answer views
        // BEGIN modified code
        int counter = 0;
        // END modified code
        for (String s : answerInfo.keySet()) {
            cb = new AnswerView(this, s, answerInfo.get(s));
            // BEGIN modified code
            cb.setId(counter);
            counter++;
            // END modified code
            answers.add(cb);
            String text = cb.getText();
            answerList.addView(cb);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit: I have solved this by myself. See my answer below I have set
Edit: Worked out the problem - see accepted answer for a good explanation! My
EDIT: See this in action here: http://jsbin.com/emobi/5 -- and that's using mouseenter/mouseleave. I have
Intro: EDIT: See solution at the bottom of this question (c++) I have a
EDIT: I would really like to see some general discussion about the formats, their
EDIT: This problem has been solved. See below. Hey all. I'm building an iPhone
I receive this message (see image below) when I try to edit in debugging.
EDIT: See my working code in the answers below. In brief: I have a
I have a Controller with two Edit methods (see below). When I submit the
See updated input and output data at Edit-1. What I am trying to accomplish

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.