I have to implement a functionality in which I have to store multiple id's in SharedPreferences in an application in android. I have to perform three main operations on data in preferences
1. add and save new id
2. delete a particular id
3. check if id exists
I wrote following class to perform all operations needed.
public class PreferenceUtils {
Context context;
private static final String TAG = PreferenceUtils.class.getName();
private static final String FAVOURITES = "favourites";
SharedPreferences preferences;
SharedPreferences.Editor editor;
public PreferenceUtils(Context context) {
this.context = context;
preferences = PreferenceManager.getDefaultSharedPreferences(context);
editor = preferences.edit();
}
public void save(long id) {
Set<String> prefStrings = preferences.getStringSet(FAVOURITES, new HashSet<String>());
prefStrings.add(id+"");
editor.putStringSet(FAVOURITES, prefStrings);
editor.commit();
editor.clear();
Log.d(TAG,id + " saved");
}
public void delete(long id) {
Set<String> prefStrings = preferences.getStringSet(FAVOURITES, new HashSet<String>());
prefStrings.remove(id + "");
editor.putStringSet(FAVOURITES, prefStrings);
editor.commit();
editor.clear();
Log.d(TAG,id + " deleted");
}
public boolean isExists(long id) {
final Set<String> prefStrings = preferences.getStringSet(FAVOURITES, new HashSet<String>());
return prefStrings.contains(id+"");
}
public Set<String> getAll() {
return preferences.getStringSet(FAVOURITES, new HashSet<String>());
}
public void clearHistory() {
editor.clear();
editor.commit();
}
}
I am creating instance of PreferenceUtils class from MainActivity like this:
PreferenceUtils pref = new PreferenceUtils(getApplicationContext());
Now the problem is when I am saving few values in preferences and closing application using back button or a Quit button (which will finish() MainActivity) everything is working fine and I am getting all the values from preferences. However, if I am force closing the application and reopening it I am getting only the first value I saved and rest all values are lost.
Try to remove the editor.clear() in both save and delete.this might be your problem.
Related
I want to clear the data I am getting from SharedPreference, I try the following answer but didn't make my task:
1) how to delete sharedpreferences ,Quit and launch application from first actvity in android
2) clear the value of sharedpreferences
3) Remove Shared preferences key/value pairs
4) SharedPreferences Clear/Save
they are all removing value, after they write data into SharedPreference, like editor.remove and .clear...
I have write data into SharedPreference in Notification Activity Like this:
public static final String PREFS_NAME = "com.example.sociapp";
NotificationAdapter notificationAdapter1 = new NotificationAdapter(NotificationsActivity.this, NotificationList, NKeyList);
RnotificationList.setAdapter(notificationAdapter1);
isthereItem = notificationAdapter1.getItemCount();
Toast.makeText(NotificationsActivity.this, ""+isthereItem, Toast.LENGTH_SHORT).show();
//writing data into SharedPreference
SharedPreferences.Editor editor = settings.edit();
editor.putInt("changingicon",isthereItem);
//editor.commit();
editor.clear();
editor.apply();
And I am getting this int value in MainActivity Like this:
SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
// Reading from SharedPreferen
try {
//all I want to refresh below line everytime I start MainActivity.
int ChangeIcon = settings.getInt("changingicon", 0);
if (ChangeIcon == 0)
{
int valuebecomes = 0;
notificationIconSetting(valuebecomes);
}
else
{
int valuebecomes = 1;
notificationIconSetting(valuebecomes);
}
Toast.makeText(MainActivity.this, ""+ChangeIcon, Toast.LENGTH_SHORT).show();
}
catch (ClassCastException e){e.printStackTrace();}
The method I call, when I get int value from SharedPreference:
private void notificationIconSetting(int IconTochange)
{
if (IconTochange == 0) {
navigationView.getMenu().getItem(2).setIcon(R.drawable.notification);
}
else
{
navigationView.getMenu().getItem(2).setIcon(R.drawable.notificationalert);
navigationView.setItemIconTintList(null);
}
}
Actually I am getting an int value greater than 0 when there is a notification in the adapter, and when there is no notification in the adapter the int value is equal to 0, then I am using this value to change the notification icon.
When there is notification:
When there is no notification:
Now the problem is whenever I get a value, it remains the same until I clear app cache or Uninstall and then install again.
All I want to refresh the SharedPreference value every time I start MainActivity.
You want to remove one key/value from shared preference
here's how i do it.
public void clearSpecificKey(String key){
sharedPreferences.edit().remove(key).apply();
}
Few things to note :
You should create a generic class of Shared Preference Like below
public class SharedPrefs {
private static final String MY_PREFS_NAME = "YOUR-PREFERENCE-NAME";
private static SharedPreferences sharedPreferences;
private String masterKeyAlias;
public SharedPrefs(Context context) {
{
try {
masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
try {
sharedPreferences = EncryptedSharedPreferences.create(MY_PREFS_NAME,masterKeyAlias,context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM);
} catch (GeneralSecurityException | IOException e) {
e.printStackTrace();
}
}
public String getStrings(Context mContext, String key){
return sharedPreferences.getString(key, null);
}
public void putString(Context mContext, String key, String value ){
sharedPreferences.edit().putString(key, value).apply();
}
public boolean getBoolean(Context mContext, String key){
return sharedPreferences.getBoolean(key, false);
}
public void putBoolean(Context mContext, String key, boolean value ){
sharedPreferences.edit().putBoolean(key, value).apply();
}
public static void clear(Context mContext){
sharedPreferences.edit().remove("user").apply();
}
public void clearSpecificKey(String key){
sharedPreferences.edit().remove(key).apply();
}
}
Here how to use it
Declaration :
SharedPrefs sharedPrefs;
Initialization :
sharedPrefs = new SharedPrefs(context);
just call the methods you want to use to store value in shared preference like
sharedPrefs.putString(context,key,value)
masterKeyAlias is to secure my Shared preferences.
Add this your app gradle
implementation "androidx.security:security-crypto:1.0.0-beta01"
you can read more about it here Best Practices
Hi I'am new to android.
I want to create a shared preferences when the app is first installed and the insert some data.
The shared preference has to be used from all activities in the app.
I tried creating the shared preference in the onCreate() function of the first activity and inserted values into it.And edited the data from another activity.
But when I restart the app the shared preference changes to the data give in the
onCreate().
Can somebody help me?
The way if you want to insert data once:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("myPref", MODE_PRIVATE);
boolean shouldInsertData = preferences.getBoolean("shouldInsertData", true);
if(shouldInsertData){
//insert your data into the preferences
preferences.edit().putBoolean("shouldInsertData", false).apply();
}
}
Share Preference is best way to store short information. but if you will create share preference from an active, it may create problem to access it from other activity.
You should create a global common share preference from application so you can access it through out the android project, any where in any activity.
I get reference from here -
It is 4 step process.
Step 1- create a java class file name as "myapp" and extend it by application.
public class MyApp extends Application {
private static Context context;
private String TAG ="myApp";
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
// Log.e(TAG, " myapp stater");
}
public static Context getContext(){
return context;
}}
Step 2 - in android manifest file inside application tab add android:name=".myapp"
Step 3 - Create java class name as "SharePreferenceUtils" (note don't use name SharePreference.)
public class SharePreferenceUtils {
private static String PREFERENCE_NAME = "shopeasy-ecommerce";
private static SharePreferenceUtils sharePreferenceUtils;
private SharedPreferences sharedPreferences;
private SharePreferenceUtils(Context context){
PREFERENCE_NAME = PREFERENCE_NAME + context.getPackageName();
this.sharedPreferences = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
}
public static SharePreferenceUtils getInstance(){
if (sharePreferenceUtils == null){
sharePreferenceUtils = new SharePreferenceUtils(SplashActivity.getContext());
}
return sharePreferenceUtils;
}
// login response user_id 1234
public void saveString(String key, String Val ){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, Val);
editor.commit();
}
public String getString(String key, String defVal){
return sharedPreferences.getString(key, defVal);
}
public String getString(String key){
return sharedPreferences.getString(key, "");
}
public void saveInt(String key, int Val ){
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, Val);
editor.commit();
}
public int getInteger(String key){ return sharedPreferences.getInt(key, 0 ); }
/**
* Clear all values from this preference
*/
public void clear() {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.commit();
}
/**
* Clear value of given key from this preference
*
* #param key name of the key whose value to be removed
*/
public void clearString(String key) {
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(key);
editor.commit();
}}
Step 4 Call sharePreferenceUtil from activity
SharePreferenceUtils.getInstance().saveString("Username", "username value here");
Now you can access sharepreference from any activity. Just call instance of sharepreference java class.
public abstract class CartSharedPrefrences {
public static boolean addFavoriteItem(Activity activity,String favoriteItem){
//Get previous favorite items
String favoriteList = getStringFromPreferences(activity,null,"favorites");
// Append new Favorite item
if(favoriteList!=null){
favoriteList = favoriteList+","+favoriteItem;
}else{
favoriteList = favoriteItem;
}
// Save in Shared Preferences
return putStringInPreferences(activity,favoriteList,"favorites");
}
public static String[] getFavoriteList(Activity activity){
String favoriteList = getStringFromPreferences(activity,null,"favorites");
return convertStringToArray(favoriteList);
}
private static boolean putStringInPreferences(Activity activity,String nick,String key){
SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, nick);
editor.commit();
return true;
}
private static String getStringFromPreferences(Activity activity,String defaultValue,String key){
SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
String temp = sharedPreferences.getString(key, defaultValue);
return temp;
}
private static String[] convertStringToArray(String str){
String[] arr =str.split(",");
return arr;
}
}
i have tried out above solution, while debugging it saves data to shared
preferences but when i am trying to retrieve data from shared preferences it
will returning null, while adding data to shared preferences it is retrieving
the method to check if previously any data available in shared preferences or
not, at that time that method it returning the data for checking purpose, but
while calling from another activity it is returning null. can anyone help me?
how to get the perfect values from it. thanks in advance.
You are using a private mode :
SharedPreferences sharedPreferences = activity.getPreferences(Activity.MODE_PRIVATE);
If you want to share the preferences between activities, you could use :
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
I can't obtain data from SharedPreferences.. After I click on a button, it executes AsyncTask in a different class named Background.
public class Background extends AsyncTask<Integer,Void,String>{
private Context context;
private AsyncResponse listener;
public Background(Context context,AsyncResponse listener) {
this.context = context;
this.listener=listener;
}
AsyncResponse is an interface that I've created to inform my MainMactivity that background work has been finished. It's just:
package com.example.pablo.zad3;
public interface AsyncResponse {
void TaskCompleted();
}
Then I want to pass the result to SharedPreferences:
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
SharedPreferences prefs = this.context.getSharedPreferences("MyPrefs",Context.MODE_PRIVATE);
prefs.edit().putString("A", s);
prefs.edit().commit();
listener.TaskCompleted();
}
But in my MainActivity I can't get the resulting string, it's like there was no A key in SharedPreferences (I always get "NO DATA"):
#Override
public void TaskCompleted() {
SharedPreferences prefs = this.getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
if(prefs != null) {
String text = prefs.getString("A", "NO DATA");
editText2.setText(text);
}
}
I don't know what I'm doing wrong, can u help me?
The problem are those two lines:
prefs.edit().putString("A", s);
prefs.edit().commit();
at the first one you are opening SharedPreferences for edit and do the changes ...
now at the second line you are opening it again ... which causing the previous changes rollback ...
As the "family" of putXXX(...) method of the SharedPreferences.Editor class returns Editor itself you should do something like:
prefs.edit().putString("A", s).putXXX(...).putXXX(...).commit();
(by putXXX(...) I mean puting other preferences fx putInt("Z", 666))
now the changes will be saved
Use this for writing to preferences:
//Write to preferences
String s = "this is a test.";
SharedPreferences prefs = this.getSharedPreferences("MyPrefs",this.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("A", s);
editor.apply();
And this for retrieving information
//Fetch from preferences
SharedPreferences prefs2 = this.getSharedPreferences("MyPrefs", this.MODE_PRIVATE);
if(prefs2 != null) {
String text2 = prefs.getString("A","");
Log.d(LOG_TAG, "This is the string: "+text2);
}
The only thing that you missed is an editor.
I am trying to update a password generator I made to include the ability to save passwords, so I'm using SharedPreferences. I want to put the saved passwords inside a Set, but when I try to save the set using SharedPreferences.Editor's putStringSet method, Eclipse does not recognize it. When I hover over putStringSet in my code, the 2 quick fixes available are "Change to putString(...)" and "add cast to editor", but I don't think either of those helps.
This is what I have:
public void save(View v)
{
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
final SharedPreferences.Editor editor = prefs.edit();
savedPasswords = (Set<String>) getSharedPreferences("savedPasswordsList", 0);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setItems(passwords, new DialogInterface.OnClickListener(){
public void onClick(DialogInterface face, int num)
{
savedPasswords.add(passwords[num]);
editor.putStringSet("savedPasswordsList", savedPasswords);
editor.commit();
refreshSavedPasswordsList();
}
});
dialog.show();
}
putStringSet(...) was added at API 11 (Android v3.0.x onwards). My guess is you're targeting a version below that.
I implemented data storage using putStringSet and then needed to backport it to Gingerbread so I created a small class called JSONSharedPreferences.
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.content.SharedPreferences;
public class JSONSharedPreferences {
private static final String PREFIX = "json";
public static void saveJSONObject(SharedPreferences prefs, String key, JSONObject object) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
editor.commit();
}
public static void saveJSONArray(SharedPreferences prefs, String key, JSONArray array) {
SharedPreferences.Editor editor = prefs.edit();
editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
editor.commit();
}
public static JSONObject loadJSONObject(SharedPreferences prefs, String key) throws JSONException {
return new JSONObject(prefs.getString(JSONSharedPreferences.PREFIX+key, "{}"));
}
public static JSONArray loadJSONArray(SharedPreferences prefs, String key) throws JSONException {
return new JSONArray(prefs.getString(JSONSharedPreferences.PREFIX+key, "[]"));
}
public static void remove(SharedPreferences prefs, String key) {
if (prefs.contains(JSONSharedPreferences.PREFIX+key)) {
SharedPreferences.Editor editor = prefs.edit();
editor.remove(JSONSharedPreferences.PREFIX+key);
editor.commit();
}
}
}
Usage:
//Below, the code to use putStringSet is commented out.
//Alternative JSONSharedPreferences is used instead
//Set<String> trainers = new TreeSet<String>();
JSONArray jTrainers = new JSONArray();
List<FilteredWorkoutVideo> videos = getAllFilteredVideos(prefs);
for (FilteredWorkoutVideo video : videos) {
//trainers.add(video.getTrainerName());
jTrainers.put(video.getTrainerName());
}
//e = prefs.edit();
//e.putStringSet(Constants.KEY_ALL_TRAINERS, trainers);
//e.apply();
JSONSharedPreferences.saveJSONArray(prefs, Constants.KEY_ALL_TRAINERS, jTrainers);
To work around this issue I created a SharedPreferencesCompat class:
In stores the StringSet in a string CSV-style.
It is possible to change ',' used in CVS by another delimiter.
public class SharedPreferencesCompat {
private final static String KEY_DELIMITER = "com.example.delimiter";
public static void setStringSetDelimiter(final SharedPreferences sharedPreferences, final String delimiter) {
final Editor editor = sharedPreferences.edit();
editor.putString(KEY_DELIMITER, delimiter);
editor.commit();
}
public static Set<String> getStringSet(final SharedPreferences sharedPreferences, final String key) {
final Set<String> out = new LinkedHashSet<String>();
final String base = sharedPreferences.getString(key, null);
if (base != null) {
out.addAll(Arrays.asList(base.split(sharedPreferences.getString(KEY_DELIMITER, ","))));
}
return out;
}
public static void putStringSet(final SharedPreferences sharedPreferences, final String key,
final Set<String> stringSet) {
final String concat = StringUtils.join(stringSet, sharedPreferences.getString(KEY_DELIMITER, ","));
final Editor editor = sharedPreferences.edit();
editor.putString(key, concat);
editor.commit();
}
}
Note:
It depends on apache StringUtils.join() method.
You can easily swap this out for another one.
I know this thread is a old, but I just ran into the same issue and my solution was to use a global HashTable to keep track of my strings, which were actually file names. I also needed a status associated with with each file, so I implemented the following simple class:
public class Globals
{
public static enum SendStatus
{
NOT_SENT,
SEND_SUCCESS,
SEND_ERROR
}
public static volatile Hashtable<String, SendStatus> StoredFilesStatus;
}
}