1

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);
}
CRUSADER
  • 5,486
  • 3
  • 28
  • 64
Linn
  • 121
  • 1
  • 9
  • Use saveInstaceState for Saving your arrayList refer this http://stackoverflow.com/a/15362869/1935890 – Dixit Patel Jun 25 '13 at 09:00
  • I think application lifecycle is a good place to start. If I remember right, but its some time ago. rotation triggers it. – Wouter van der Houven Jun 25 '13 at 09:00
  • @DixitPatel The ArrayList is stored in saveInstanceState. The list itself is saved and loaded perfectly fine. It's just the actual images that doesn't appear again – Linn Jun 25 '13 at 09:09
  • @Linn do u find the solution?? – Avadhani Y Jun 27 '13 at 05:52
  • @AvadhaniY Unfortunately nothing substantial. I'm doing a workaround now where I completely kill the activity and save the ArrayList in Shared Preferences. On start I retrieve Shared Preferences, loop through the ArrayList and dynamically add the ImageViews again. It works, but it's not ideal. – Linn Jun 27 '13 at 09:32

4 Answers4

1

You need to create Map instead of List and then do something like :

map.put(imageViewId,R.drawable.imageViewResource);

Then after pause just do :

RelativeLayout rl = (RelativeLayout) findViewById(R.id.top);
ImageView imageView = (ImageView)rl.findViewById(imageViewId);

Integer imageViewResource = map.get(imageViewId);
imageView.setImageResource(imageViewResource);

If this will not update your image try to :

        imageView.post(new Runnable() {
        @Override
        public void run()
        {
             imageView.setImageResource(imageViewResource);
        }
    });

If your map is empty after rotation and you haven't time to play with save/restore instance state, just make map static.

Best wishes.

Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
0

Declare your imageview variable within the scope of the method:

ImageView iv = new ImageView(this);

You cannot reuse the variable once it is added.

Neoh
  • 15,906
  • 14
  • 66
  • 78
0

Add configChanges for that activity in manifest

    <activity name="Your ClassName with Package" android:configChanges="orientation"/>

and call your method AddImage() in OnCongigurationChanged as below:

 @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    int ot = getResources().getConfiguration().orientation;
    switch (ot) {
    case Configuration.ORIENTATION_LANDSCAPE:
        AddImage();
        break;
    case Configuration.ORIENTATION_PORTRAIT:
        AddImage();
        break;
    }
}

@SuppressWarnings("deprecation")
@Override
public Object onRetainNonConfigurationInstance() {
    return super.onRetainNonConfigurationInstance();
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
-1

Add configChanges for that activity in manifest

    <activity
    ....
    android:configChanges="orientation" 
    .... >
blganesh101
  • 3,647
  • 1
  • 24
  • 44