Reputation: 43
I have a SharedPreferences that activates interstitial after every third action within the application. I want SharedPreferences to clear the settings when a user leaves the application so that interstitial can load after every application startup. Please help.
SharedPreferences admob = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = admob.edit();
// Save counter value back to SharedPreferences
editor.putInt(COUNTER_INTERSTITIAL_ADS, mAdCounter);
editor.apply();
Upvotes: 0
Views: 1312
Reputation: 4514
Put this dependency in your build.gradle
file:
implementation "android.arch.lifecycle:extensions:*"
Then in your Application class, use this:
public class MyApplication extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
@OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
private void onAppDestroyed() {
//Clear data here
SharedPreferences pref = getSharedPreferences(PREF_ID, Context.MODE_PRIVATE);
pref.edit.clear().apply();
}
@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
Log.d("MyApp", "App in foreground");
}
}
Update your AndroidManifest.xml file:
<application
android:name=".MyApplication"
....>
</application>
Upvotes: 1
Reputation: 29814
I have a SharedPreferences that activates interstitial after every third action within the application. I want SharedPreferences to clear the settings when a user leaves the application so that interstitial can load after every application startup. Please help.
Instead thinking backward and trying to clean up the SharedPreferences
at the end of your Application, why don't you just reset the SharedPreferences
in startup? It's more cleaner solution for your case.
You can't depend on onDestroy()
method to clean up the SharedPreferences because there's no guarantee that it will be called when the application is terminated.
So, you can solve the problem easily by reset the SharedPreferences when you start your related Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
SharedPreferences pref = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
// reset the counter
editor.putInt(COUNTER_INTERSTITIAL_ADS, 0);
editor.apply();
...
}
Upvotes: 0
Reputation: 1
protected void onDestroy() {
super.onDestroy();
SharedPreferences admob = getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = admob.edit();
editor.clear();
editor.apply(); // or editor.commit() if on older API versions
}
Upvotes: 0
Reputation: 4039
Put your cleanup(Shared Preferences Cleanup) code in the onDestroy()
method, this is the last method that is called before an activity finally closes. For all the information on the Android activity life cycle have a look at the Android Documentation for: Activity Lifecycle.
Sample Code:
SharedPreferences pref = getSharedPreferences(PREF_ID, Context.MODE_PRIVATE);
pref.edit.clear().apply();
Upvotes: 2
Reputation:
Put your code on the onDestroy()
as @janDe said. Indeed it is the last method that gets called before a activity is "destroyed". The code to delete all of your shared preferences is this:
SharedPreferences pref = getSharedPreferences(PREF_ID, Context.MODE_PRIVATE);
pref.edit.clear().apply();
Basically what you do is get the preferences, go into edit mode(where you can edit the values) and say clear()
. Then you can apply the changes.
Upvotes: 0