How to delete shared preferences data from App in Android
To remove specific values: SharedPreferences.Editor.remove() followed by a commit()
To remove them all SharedPreferences.Editor.clear()
followed by a commit()
If you don't care about the return value and you're using this from your application's main thread, consider using apply()
instead.
Android delete SharedPreferences
I post you and example, please try this code. It should works:
// Declare your shared preference file name
private static final String PREF_NAME = "MyPrefs";
For deleting all the preferences:
SharedPreferences sharedPrefs = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.clear();
editor.commit();
For reading:
SharedPreferences sharedPrefs = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
String prefString = sharedPrefs.getString(key, "");
For writing:
SharedPreferences sharedPrefs = context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(key, "someValue");
editor.commit();
How to delete SharedPreferences automatically after one month?
It depends.
The easiest way is to delete it when the user starts the app.
When the apps is created, you check the SharedPreferences for the last updated time.
If it's null (the first time), you save the current time in milliseconds as a long.
If it's not null, you read it and compare it against the current time. If it less than a month, you do nothing. If it's more than a month, you clear the shared preferences and, after clearing it, insert the new time.
Something like:
long lastUpdate = sharedPreferences.getLong(LAST_UPDATE, -1);
if(lastUpdate == -1) {
//First time
sharedPreferences.edit().putLong(LAST_UPDATE, System.currentTimeMillis()).apply();
} else {
boolean isMoreThanAMonth = //Here you should do the math. it depends, you want to consider a month like 30 days, or you want to know if it was in another month... somehthing like that
if(isMoreThanAMonth) {
sharedPreferences.edit().clear().apply()
}
}
Of course, if you want to clear the SharedPreferences even if the user does not open the app you should use a Service. It's more complex and expensive for the OS, so you should try to go for the first one if it fits your requirement.
Can we delete SharedPreferences when closing the app
It would be possible to clear SharedPreferences
every time, either when the main activity is destroyed or when the app is next run. However, as @CommonsWare points out, doing this would miss the whole point of SharedPreferences
. (And check out @CommonsWare's reputation to see whether or not to believe them.) The purpose of SharedPreferences
is to store values to use the next time the app is run. If you clear them automatically every time...
You can just use a variable to store your data. This variable will naturally be cleared every time the app closes.
SharedPreferences data not deleting
You need to chain request together:
sp.edit().remove(ids).apply();
Alternatively you can do something lie:
editor = sp.edit();
editor.remove(ids);
editor.apply()
Is it possible to clear data from shared preference when app is cleared from recent list.?
As already said, there is no guarantee that you will receive a callback when you app is killed no matter what callback you try to use. However, I'm still wondering why you are using the SharedPreferences
if you do not want that data to be persistent, it's beside the point of this class. That being said, you should handle the data like any other data inside your app.
If you are required to use the SharedPreferences
for some reason, then the proper way to clear it is when you start the application, not when you kill it. This way, you won't have any problems.
Clear shared preference having dynamic keys
Removing all preferences:
SharedPreferences sharedPreferences = context.getSharedPreferences("name", Context.MODE_PRIVATE);
sharedPreferences.edit().clear().commit();
Removing single preference:
SharedPreferences sharedPreferences = context.getSharedPreferences("name", Context.MODE_PRIVATE);
sharedPreferences.edit().remove("key_name").commit();
SharedPreferences
has the method getAll()
that returns a Map<String, ?>
. From the Map you can retrieve easily the keys with keySet()
and the key/value mappings with entrySet()
:
Map<String, ?> allEntries = prefA.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());
}
Related Topics
Android Fastboot Waiting for Devices
How to Check If a Value Exists Already in a Firebase Data Class Android
Install Apps Silently, With Granted Install_Packages Permission
Android: Open a Pdf from My App Using the Built in Pdf Viewer
How to Prevent User Go Back to Protected Page After Logout
Import Android.Support.V7.App Cannot Be Resolved
Android Kill Previous Activities Without a New Intent
Asking Camera Permission for Webview At Runtime
How to Manage Startactivityforresult on Android
How to Set Default Font Family For Entire Android App
How to Set Align (Right or Left) of Text in Textview, Programmatically
How to Disable Preveious Date in a Date Picker
How to Check Miui Autostart Permission Programmatically
Force Override Camera Image on Android
How to Get Root Access on Android Emulator
Download Manager Progress Not Visible in the Notification Area
Flutter:Renderbox Was Not Laid Out: Renderrepaintboundary#58C65 Relayoutboundary=Up1 Needs-Paint