I have a method in my main class, that fetches some data from the internet. The thing is that after everything is done, if I change the screen orientation by moving the device, everything starts allover again(fetching data while displaying a loading screen). Is there somewhere I could put my method so that if my device's screen orientation changes, it won't erase everything that has been done until that moment? Thanks.
-
[Handling Runtime Changes](http://developer.android.com/guide/topics/resources/runtime-changes.html) – Kevin Coppock Oct 17 '12 at 14:48
-
if you put it as an answer I'll chose it as the correct one – Teo Oct 17 '12 at 15:00
3 Answers
What is happening to you is that every time you rotate your activity is recreated, as per android good practices you should handle your activity being recreated because android may destroy your activity at any point if resources go low on the device. Take a look at saving the state of your activity and how to restore it and the link.

- 1
- 1

- 1,818
- 1
- 16
- 26
You can use a singleton class to store your data. If you prefer a simpler way you can also put your data as static, so the orientation change will not throw them away.

- 1,628
- 1
- 16
- 22
I think that your Activity is getting recreated again. In that case,it will load again.
1). You can handle orientation change by overriding
public void onConfigurationChanged(Configuration newConfig)
and in your activity declaration in manifest file add the following line
android:configChanges="orientation|screenSize|keyboardHidden"
2). As Aerilys said in the above answer, you can use singleton class to store data. Before displaying your loading screen check if you single ton object has data or not. If yes then skip displaying your loading screen

- 3,001
- 6
- 32
- 65