1

Possible Duplicate:
Android : Save application state on screen orientation change

In my app I have a listView. When starts application it automitacally loaded all phone contacts that exists in that phone. So when rotate my phone it's start load again, every time I rotate device it is start reading again and again. What should I do here, so I the contacts load just one time and even I rotate the phone.

Community
  • 1
  • 1
user989340
  • 123
  • 1
  • 2
  • 9
  • 2
    At lest serach for such question once on SO http://stackoverflow.com/questions/8093213/android-save-application-state-on-screen-orientation-change/8093272#8093272 – ingsaurabh Nov 14 '11 at 10:18

4 Answers4

4
android:configChanges = "orientation"

add this tag in your activity in manifest file.

Yashwanth Kumar
  • 28,931
  • 15
  • 65
  • 69
1

Return the data in onRetainNonConfigurationInstance() and retrieve it in your onCreate() using getLastNonConfigurationInstance()

If getLastNonConfigurationInstance() returns null, request the data again.

These two functions are designed to be used to save data between instances of the same activity being recreated because of a configuration change like orientation.

As mentioned, you can alternatively stop Android destroying / recreating your activity on such a configuration change by adding the events you want to manually handle to android:configChanges = "XXX" in the activity tag in your manifest. This is suitable if you don't require different assets / layouts etc. when in portrait and landscape. However, if you only add orientation to configChanges, you might experience the same problem when another event triggers the activity to be recreated - check out: Handling run time changes

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
0

You should use a 'Content Provider'. The provider will handle loading and will handle rotaions, onPauses and such. I made a frontend for a spanish digg like page and had the same problem. Here is the project so you can have a look at how you can solve the problem: http://code.google.com/p/meneameandroid/

Moss
  • 6,002
  • 1
  • 35
  • 40
0

Wanted to add some more to Yashwanth Kumar's ans. like you need to Override the OnConfigurationChange method in the activity where you are loading the content.

Dinesh Prajapati
  • 9,274
  • 5
  • 30
  • 47
  • if you do so. when your device is rotated you will get this method executed first other then any other methods. so you can save your ojects. – Dinesh Prajapati Nov 14 '11 at 10:28