Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 928
y ramesh rao Avatar asked Apr 07 '10 05:04

y ramesh rao


2 Answers

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.

like image 128
rascalking Avatar answered Oct 18 '22 12:10

rascalking


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.

like image 34
IvanRF Avatar answered Oct 18 '22 13:10

IvanRF