1

I am a French noob in Android and I have a problem which I do not find a solution. In fact, I can't save my LinearLayouts'states during my tablet's rotation. I have tried a lot of things and I made this code:

 public class Repas extends Activity {

    private LinearLayout area1, area2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repas);
        area1 = (LinearLayout) findViewById(R.id.area1);
        area2 = (LinearLayout) findViewById(R.id.area2);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        final int width_screen=dm.widthPixels;
        area2.setMinimumWidth(width_screen);

        if( savedInstanceState != null ) {
            int count_item = savedInstanceState.getInt("number_of_item");
            String image = savedInstanceState.getString("Sauvegarde_repas0");
            //area2.addView(image);

        }
        else {
            TypedArray arrayResources = getResources().obtainTypedArray(
                    R.array.resicon);

            for (int i = 0; i < arrayResources.length(); i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageDrawable(arrayResources.getDrawable(i));
                imageView.setOnTouchListener(myOnTouchListener);
                area2.addView(imageView);
            }

            arrayResources.recycle();
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState)
{
    int count = area1.getChildCount();
    View v = null;
    outState.putInt("number_of_item", count);

for(int i=0; i<count; i++) {
    v = area1.getChildAt(i);
    outState.putString("Sauvegarde_repas"+i, v.toString());
}
super.onSaveInstanceState(outState);
}

My current problem, assuming my logic is right, is I cannot convert my String into ImageView during my tablet's rotation. Can you help me ?

I would also like to know if what I did is right: I mean To got my layout's elements one by one(I didn't find how to save the whole layout)?

I hope I am clear enough : if you need more information, don't hesitate !

Thank you very much for your help !

  • http://developer.android.com/guide/topics/resources/runtime-changes.htmlhttp://stackoverflow.com/questions/3915952/how-to-save-state-during-orientation-change-in-android-if-the-state-is-made-of-m http://stackoverflow.com/questions/12214600/save-data-and-change-orientation – Chaudhary Amar Apr 21 '16 at 07:02
  • Thank you for your answer! As you can see, I already use onSavedInstanceState.... – ValentinLoricourt Apr 21 '16 at 12:09

1 Answers1

0

I make a new post because my code is working... but I have several questions that I would be very grateful if you can answer them.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.repas);
        area1 = (LinearLayout) findViewById(R.id.area1);
        area2 = (LinearLayout) findViewById(R.id.area2);


        TypedArray arrayResources = getResources().obtainTypedArray(
                R.array.resicon);

        if( savedInstanceState != null ) {
            int count_item = savedInstanceState.getInt("number_of_item");
            int tab_item[] = new int[count_item];

            for (int i = 0; i < count_item; i++) {
                tab_item[i]=savedInstanceState.getInt("Sauvegarde_repas" + i);
            }

            for (int i = 0; i < arrayResources.length(); i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageDrawable(arrayResources.getDrawable(i));
                imageView.setOnTouchListener(myOnTouchListener);
                imageView.setId(i);


                if (check(tab_item,i)){
                    area1.addView(imageView);
                }
                else{
                    area2.addView(imageView);
                }
            }
        }
        else {

            for (int i = 0; i < arrayResources.length(); i++) {
                ImageView imageView = new ImageView(this);
                imageView.setImageDrawable(arrayResources.getDrawable(i));
                imageView.setOnTouchListener(myOnTouchListener);
                imageView.setId(i);
                area2.addView(imageView);
            }

            arrayResources.recycle();
        }

        area1.setOnDragListener(myOnDragListener);
        area2.setOnDragListener(myOnDragListener);
    }

    @Override
    public void onSaveInstanceState(Bundle outState)
    {
        int count = area1.getChildCount();
        View v = null;
        outState.putInt("number_of_item", count);
        for(int i=0; i<count; i++) {
            v = area1.getChildAt(i);
            outState.putInt("Sauvegarde_repas" + i, v.getId());
        }
        super.onSaveInstanceState(outState);
    }

Here are my interrogations:

  • When I set an id, it's just "i", a very simple number. It doesn't seem very unique like a string "R.id.String_here" (even if I know that is converted in Integer), so how can I be sure to do a unique ID?

  • Do I have to save, one by one, the child of my layout? It's not possible to save the whole layout?

    • Do you have any advice to improve my code? (Because it looks dirty, isn't it ?)

Thank you very much for your help !