开发者

Android ListView not retaining values through orientation change

开发者 https://www.devze.com 2023-03-11 03:13 出处:网络
I am a bit perplexed on what Android holds onto in a view orientation change and what it doesn\'t. Some things it seems it holds onto nicely, other t开发者_运维技巧hings it seems it doesn\'t.. So I a

I am a bit perplexed on what Android holds onto in a view orientation change and what it doesn't.

Some things it seems it holds onto nicely, other t开发者_运维技巧hings it seems it doesn't.. So I am not sure if it is my code, or something else. (I assume I am doing something stupid)

What I have is a view with a listview in it, the listview has a simple adapater and an array of map items to put into the listview.

When the orientation changes, I know that the activity is destroyed and recreated, so I assume that my simple adapater and its tie to the listview are gone.. so the listview won't rotate populated and I'll need to use the onRetainNonConfigurationInstance or onSaveInstanceState methods to pass through either the adapter or the list of values?

This seems excessive, I can't believe the sharp guys at Google don't have an easier way to handle this.. The listview has an ID, and after orientation I can add items to it and it works fine, but it just doesn't hold onto the values it had before.

Is my only way around this to pass the list content through explicitely? Is it the fact I am creating a new SimpleAdapter and linking it to the list in the onCreate that is causing the issue? Or is it the fact the array of Items I have linked to the adapter is whiped out when the activity is?? This seems like something that shouldn't require this much work to accomplish.. am I missing something?


No, you could also have a persistent model through Activity lifecycle like this :

define a singleton app, with a method to get your singleton model.

At on create get your datas from your model.

As your model will no be recreated but will persist, this can be used as a workaround for preserving objets through rotation change.

Regards, Stéphane

0

精彩评论

暂无评论...
验证码 换一张
取 消