views:

497

answers:

4

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.

+1  A: 

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.

Dominik Fretz
I think registering the Service is the Best Alternative is that so.
y ramesh rao
Where Can I get More Insight into Android Services for Serving my purpose
y ramesh rao
+4  A: 

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.

rascalking
I don't think you need to override onConfigurationChanged(). Adding android:configChanges should be enough.
janfsd
OMG Thats the Biggest Relief That I can Ever Imagine... You have no idea how did you just Saved My Life... Thanks a tonn For the WonderFul insight that you just gave to Me..
y ramesh rao
Come to think of it, janfsd's probably right...if you're not doing anything useful in onConfigurationChanged, just let the default implementation do that.
rascalking
+1  A: 

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/

Moss
A: 

I want to lock the orientation of the screen to portrait when hard keyboard is not open and want the orientation of the screen to landscape when hard keyboard is open. The only difference between normal behavior and in mine that I don't want the orientation to be changed when screen is just rotated and hard keyboard is not open. Please suggest.

Vishal
Start your own question. Answers are for answering not to pose additional questions.
Christian