Reputation: 12544
My application should load some data when it start at first time, but it should do not load again when it resumes, when I do that loading on onCreate
methods, if user changes orientation, that process do again, I do not want this, I need to run loading only once, is there any way to fix it?
Upvotes: 2
Views: 2018
Reputation: 864
It's really simple! Just put:
android:configChanges="keyboardHidden|orientation"
in the AndroidManifest.xml
file inside activity tag in which you require such functionality.
Example:
<activity
android:name=".MyActivity"
android:configChanges="keyboardHidden|orientation"
android:label="Test"
android:windowSoftInputMode="stateHidden" >
</activity>
Upvotes: 0
Reputation: 742
When a configuration change occurs at runtime, the activity is shut down and restarted by default, but declaring a configuration with this attribute (android:configChanges
) will prevent the activity from being restarted. In your case, the value for this property is
android:configChanges="orientation"
Upvotes: 0
Reputation: 54806
Just set some flag when you load the data, and only load the data when the flag is not set. For instance:
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
if (! preferences.getBoolean("dataLoaded")) {
loadData();
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("dataLoaded", true);
editor.commit();
}
This will ensure that the data is only loaded once per install. If you want to load the data once per application instance/run, then the solution is even simpler. In that case, you could just have a public static
flag somewhere instead of using SharedPreferences
.
Upvotes: 2