Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving and restoring instance state in dynamically created UI?
    primarykey
    data
    text
    <p>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)</p> <p>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.</p> <p>I've had a good look around on Google/SO for this and I've not found anything related to this problem.</p> <p>Thanks all</p> <p><strong>Edit:</strong> For anyone that comes across this in the future, I did a slightly modified version of Yakiv's approach and wound up with this:</p> <pre><code>for (int i = 0; i &lt; 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. } } </code></pre> <p>Thanks again Yakiv for the awesome idea.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload