Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove some key/value pair from SharedPreferences?

How to remove some key/value pair from SharedPreferences ? I have put and I to remove that from prefs.

like image 761
Damir Avatar asked Nov 07 '11 08:11

Damir


People also ask

How remove data from SharedPreferences in flutter?

simply use clear() function with your variable it will clear all the shared preferences. SharedPreferences preferences = await SharedPreferences. getInstance(); await preferences. clear();

How do you delete preferences on Android?

You use remove() to remove specific preferences, you use clear() to remove them all.

How do I pass data from one activity to another using SharedPreferences?

How to pass data from one activity to another in Android using shared preferences? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

Can SharedPreferences be hacked?

sharedPreferences arent safe.. sharedPreferences should just store config/setting-data not encrypted.. Quoting your link: "This essentially obfuscates the key so that it's not readily visible to attackers. However, a skilled attacker would be able to easily see around this strategy.


2 Answers

SharedPreferences mySPrefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = mySPrefs.edit(); editor.remove(key); editor.apply(); 

Here editor is the sharedPreferences editor.

like image 90
Yashwanth Kumar Avatar answered Oct 20 '22 16:10

Yashwanth Kumar


It is important to note that, unless you're planning on doing something with the return value of the commit() call, there is absolutely no reason for using the synchronous commit() call instead of the asynchronous apply() call.

Keep in mind that if you're calling this from the main/UI thread, the UI is blocked until the commit() has completed. This can take upwards of around 100ms as apposed to about 5ms for the apply. That may not seem like much, but if done continually throughout an application, it will certainly add up.

So, unless you're planning on doing something like this, hopefully on a separate thread:

editor.remove(String key);  boolean success = editor.commit(); if (!success) {      // do something  } 

You should instead be doing this:

editor.remove(String key);  editor.apply(); 
like image 31
SBerg413 Avatar answered Oct 20 '22 16:10

SBerg413