y ramesh rao
y ramesh rao

Reputation: 3004

Android ListView in Activity getting Refreshed on Changing Orientation

I have Activity with ListView inside it and in the onCreate method of the Activity I have code for populating the Data of the ListView this Data is a server based and so populating includes calling Network URLs. I have the ArrayAdapter of the ListView in the Same Activity Class.

Now the Issue I'am facing is that, in Rest all scenarios my Activity is behaving in a proper way but when the Orientation [ Portrait to Landscaped or other way round] is taking place the Data is Getting lost and Newer Data calls are Required to Populate the Same Old Data now this is something that is not intended out the code how should I deal with it.

Upvotes: 12

Views: 10792

Answers (5)

IvanRF
IvanRF

Reputation: 7265

For those targeting API 13 or higher, "screenSize" should also be used. If that is your case, add

android:configChanges="orientation|screenSize"

to your Android manifest.

More information here.

Upvotes: 13

Shyam James
Shyam James

Reputation: 39

android:configChanges="orientation|screenSize"

this code will work only if we don't have another layout for landscape mode!

Upvotes: 1

Moss
Moss

Reputation: 6012

Why not saving any data into a parceable and if the bundle you get in onCreate contains a saved state of data re-set the list adapter? Here is a nice code sample on this: http://blog.cluepusher.dk/2009/10/28/writing-parcelable-classes-for-android/

Upvotes: 1

rascalking
rascalking

Reputation: 3460

Android will stop and restart your activity unless you've told it you will handle orientation changes yourself. It does this so that you can specify different resources (such as layouts) that are orientation-specific (among other reasons).

You need to add android:configChanges="orientation" to your activity delcaration in your AndroidManifest.xml, and you need to override onConfigurationChanged(). I don't believe you need to do anything special inside onConfigurationChanged(), simply implementing it should do the trick.

Upvotes: 24

Dominik Fretz
Dominik Fretz

Reputation: 1378

AFAIK the whole Activity gets recreated on an orientation change! The same is true if you switch to anoter app and return back later. I would suggest to store the data is the SharedPreferences or serialize them into XML and store them.

Another possibility could be to register your own service that stores the data in memory and the activity poluplates the data from the service.

Upvotes: 3

Related Questions