I seem to be going bald trying to fix what probably is a very simple problem.
I have an activity in which users can click a button and an ImageView
is dynamically added to the screen. This ImageView
is also stored in an ArrayList
. When the user pauses the activity or the device is rotated all the images added to the layout disappears. They are still stored in the ArrayList
however. I attempted to loop through the ArrayList
to add the images to the layout again, however then I get an error thrown - the specified child already has a parent.
Below is my code for adding images to the layout and the ArrayList
public void AddImage() {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.top);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
iv = new ImageView(this);
id++;
iv.setId(id);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
iv.setImageResource(R.drawable.foo);
iv.setLayoutParams(params);
rl.addView(iv);
arrayList.add(iv);
}