1

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.

Teo
  • 3,394
  • 11
  • 43
  • 73

3 Answers3

1

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.

Example using onSaveInstanceState()

Community
  • 1
  • 1
Gabriel Netto
  • 1,818
  • 1
  • 16
  • 26
0

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.

Aerilys
  • 1,628
  • 1
  • 16
  • 22
0

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

Bharath
  • 3,001
  • 6
  • 32
  • 65