1

I'm creating an Android app with an activity comprised of a mixture of static views and dynamically created views. The activity contains UI elements held in an XML file and UI elements which are created dynamically based on the content of a model class. (as in, there is an array in my model class and an EditText will be created for every element in the array.. and a Spinner will be created containing every element in a different array)

What's the usual method for saving and restoring the state of the application when dynamically created UI elements are involved? Because I won't always know which UI elements will exist at any one time! Currently, my binding code simply reloads the data from the database when the device orientation changes, losing any changes that the user made.

I've had a good look around on Google/SO for this and I've not found anything related to this problem.

Thanks all

Edit: For anyone that comes across this in the future, I did a slightly modified version of Yakiv's approach and wound up with this:

for (int i = 0; i < views.size(); i++) {
        View currentView = views.get(i);
        if (currentView instanceof CheckBox) {
            outState.putBoolean("VIEW"+i, (((CheckBox) currentView).isChecked()));
        } else if (currentView instanceof EditText) {
            outState.putString("VIEW"+i, ((EditText) currentView).getText().toString());
        } else if (currentView instanceof Spinner) {
            //.....etc. etc.
        }
    }

Thanks again Yakiv for the awesome idea.

user2459186
  • 37
  • 1
  • 6
  • You were bad in your searching, here is an answer - http://stackoverflow.com/questions/151777/saving-activity-state-in-android. – Yakiv Mospan Oct 25 '13 at 10:24
  • Thank you for your link, but as I replied on Yakiv's comment, this is for the simplest case where you know which UI elements you have and can easily store them in the bundle. I won't know which UI elements I have as they are dynamically generated. The activity could have 5 edittexts or it could have 50, which makes it difficult to add these values to the bundle. – user2459186 Oct 25 '13 at 10:40

1 Answers1

1

To restore dynamic views state you need to make them as class fields and initialize them in onCreate. Then just use this article to save and restore their state.

private List<View> mViews= new ArrayList<View>();

@Override
public void onCreate(..)
{
  LinearLayout parent = ....//initialize it here
  initializeViews();
}

public void initializeViews()
{
  // create and add 10 (or what ever you need) views to the list here
  mViews.add(new View(this));
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  int mViewsCount = 0;
  for(View view : mViews)
  {
     savedInstanceState.putInt("mViewId_" + mViewsCount, view.getId());
     mViewsCount++;
  }

  savedInstanceState.putInt("mViewsCount", mViewsCount);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  int mViewsCount = savedInstanceState.getInt("mViewsCount");;
  for(i = 0; i <= mViewsCount)
  {
     View view = mViews.get(i);

     int viewId = savedInstanceState.getInt("mViewId_" + i);
     view.setId(viewId);

     mViewsCount++;
  }
}

Best wishes.

Community
  • 1
  • 1
Yakiv Mospan
  • 8,174
  • 3
  • 31
  • 35
  • Thanks for your reply. I'm a little confused though, the issue is that there could be 5 EditTexts in my activity or there could be 50.. So in 'onSaveInstanceState' I have no way of adding the values to the bundle because there won't be a unique 'key' (like 'dogColour' or 'dogName', if that makes sense) – user2459186 Oct 25 '13 at 10:38
  • This is actually a really nice solution, thank you very much for your help! I'll report back if I get any problems – user2459186 Oct 25 '13 at 10:57