Android: Dynamically created table row is being deleted on screen rotate

时间:2015-05-12 22:56:25

标签: android performance android-layout android-fragments android-activity

From what I have read online, any dynamically created views should be saved when the android screen is rotated as long as those views are given an arbitrary id.

I am running into a problem, however, where any dynamically created table row that is created is deleted when the screen changes orientation from portrait to landscape or vice versa.

I have a TableLayout defined in my main activity's fragment which is modified after an activity where the user defines the data for a new table row finishes.

So far that part works fine.

The table stores an item and a corresponding location. itemInsertResponse is called by onActivityResult, which appends the new row returned by createItemRow to the existing table.

<div ng-controller="myController">
  <div some-directive ng-model="some-directive-scope.variable" ng-init="scope.some-directive = some-directive-scope">
    {{ some-directive-scope.variable }}
  </div>
  <p ng-bind="scopes.some-directive.variable"></p>
</div>

I set id values for the table row and the text views which, from what I have read, should mean that the default implementation of OnSavedInstanceState saves them.

Is this some kind of bug, or do I actually need to manually save the newly created rows in OnSavedInstanceState in order to maintain the state of the table? If I do need to override that method, how do I save every dynamically created row?

The best solution I can think of is to store a global ArrayList of arrays containing the two strings for each row and then recreate every row in OnCreate using that ArrayList--but that's clearly very inefficient.

1 个答案:

答案 0 :(得分:1)

You shouldn't pass views when saving state. When you are saving the state, you only have to pass some generated data, for example the "_printf_" as you've mentioned and then recreate views let's say in ArrayList - if the onCreate() exists.

Btw, it seems like you don't care about that data for example if the app closes. If you would save it to a database or something then you would be able to easily read it when recreating.