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