I am trying an application where I am using Shared Preference. When I delete the preference file from data/data/com.your.package.name/shared_prefs/mySharedPref.xml manually using Android monitor, still the app is able to read the preference values.
I am assuming that some how the value is retained in main memory of the phone. Am I correct & what is the viable solution to clear shared preferences totally leaving no traces. But one thing I want to clear preference only if the file is wiped. For this I need to check presence of file, Any other approach rather than checking with File class ?
I think this code must work
public static void clearAllPreference(Context context){
SharedPreferences prefs = context.getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
}
To remove specific values: SharedPreferences.Editor.remove() followed by a commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
You use remove() to remove specific preferences, you use clear() to remove them all.
Checkout official documentation on SharedPreferences.Editor.
Related
How can I make data available through my whole android app?
For instance, I want to display a user picture in the toolbar in every screen. Also, the name of the user is displayed in the navigation drawer in every screen.
Is a singleton the right approach for this? Or are other techniques better?
I have been googling a lot, but i can't find a good approach for my use case so far.
To use shared preferences:
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
to save in shared preferences:
editor.putString("tag", "save me please");
editor.apply();
to retrieve from shared preferences:
String s = preferences.getString("tag", "default_value");
Notes:
You can make your tags as string constants to make sure you are always using the same value.
If you use the editor for multiple values, always remember to add editor.apply() in the end, to actually apply your changes.
If you want the editor to write the changes synchronously, use editor.commit() instead. You can save any type of variable in the shared preferences, just use the appropriate method (instead of putString use putInt, putLong, etc - same with the getSring). More on Shared Preferences in the API here
When unit testing shared preference, is the value get refresh every test?
public static void putString(Context context, String key, String val) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(key, val);
editor.apply();
}
Once you run your function it will save the val under the key which will be saved for every other instance. If you want to be absolutely be sure of this, you can read the value using another function (and maybe a different instance) usingPreferenceManager.getDefaultSharedPreferences(context.getString(key, "default"));
Alternatively, like many have said, you can manually check the default SharedPreferences file located in /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PACKAGE_NAME_preferences.xml inside your app data folder, on the device you are testing on.
The shared preference does not get cleared until you clear the editor. Make sure when you are testing the screen again, the editor is not getting cleared. Only then the preference may get cleared out.
To access the shared.xml folder, you can follow this link
How can I view the shared preferences file using Android Studio?
In my app, preferences are organized by SharedPreferences. I need to perform reset to application defaults.
I know how to reset all values for particular SharedPreferences:
SharedPreferences prefs = getSharedPreferences(
"name_of_the_preferences_file",
MODE_PRIVATE
);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.commit();
But the problem is that only preferences for one particular preferences "name_of_the_preferences_file" is reset. Of course, all the rest preferences are not affected.
So, how to achieve full defauls reset?
It would be good to get names of all the preferences files, to reset it one-by-one.
But ideally, I want programmatically destroy all application data. This could be the best variant.
Original answer : see Deleting shared preferences
Edit : You stated "I need to perform reset to application defaults." and that link provides the solution: you didn't mention resetting all preferences of all apps.
You cannot erase preferences from other apps from your own app because sharedpreferences are stored in a file and your app won't have write rights to directories that contain these files
I am pretty new to Android dev. I am browsing through the API here http://developer.android.com/reference/android/content/SharedPreferences.html
but I am confused about how to actually get the file contents and read or write from them.
I have this code to get the SharedPreferences object:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences( MyClassName.this);
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
Thanks!
If you want to get values from your SharedPreferneces you have to use (this example works with Strings, but you can also call getBoolean, getInt, etc..)
prefs.getString("myString", "defaultValue"); // "defaultValue" will be returned in case "myString" wasn't saved on the SharedPreferences
to store some values you can do it like this:
prefs.edit()
.putString("myString", "newValue")
.putBoolean("working", true)
.commit();
As you can see you can edit more than one value at once..
edit() will return you an editor that you have to use in order to modify the sharedpreferences file, and when you end to edit it call commit() in order to make the changes permanently
but in this case, I get the reference to sharedPreferences, but not a connection to my file which stores the preferences data.
The SharedPreferences object has a "connection" to the file which stores the preference data.
Maybe I am not understanding the API correctly, but how am I supposed to get the reference to the file and read/write to it?
To read preferences, use the getters on SharedPreferences (e.g., getString()). To write preferences yourself:
Get a SharedPreferences.Editor by calling edit() on the SharedPreferences object
Use the setters on the Editor (e.g., putString())
Call apply() (where possible) or commit() on the Editor to save your changes
In addition, you can (and in many cases should) also use a PreferenceActivity to allow users to directly view and modify their preferences.
Which of the following is/are appropriate for saving the state of an Android application?
a. Activity.onFreeze()
b. Activity.onPause()
c. Activity.onStop()
d. Activity.onDestroy()
e. Activity.onFinish()
Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.
Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.
Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.
Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.
Shared Preferences:
The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.
(1) Here is how you get the instance when you specify the file name
public static final String PREF_FILE_NAME = "PrefFile";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.
(2) The recommended way is to use by the default mode, without specifying the file name
SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);
Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:
int storedPreference = preferences.getInt("storedInt", 0);
To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
Editor also support methods like remove() and clear() to delete the preference value from the file.
Activity Preferences:
The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.
Following is the code to get preferences
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);
The code to store values is also same as in case of shared preferences.
SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();
You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.
To see some more examples check Android's Data Storage post on developers site.
Why won't you just use SharedPreferences?
Anyways: i'd store the data in onPause(), depending on what these data items are. You probably already know that all items that have an id assigned in the R.java are automatically saved onPause() and restored onResume()? - but this is more or less useless anyways since this data is lost after the app dies completely...
assuming that you want so save other stuff that you might want to write to an DB or file, onPause() might be a good choice. as you can see in http://xenonite.net/news/android-activity-lifecycle, after onPause() is called, the OS might kill the app if there's memory needed for other things. so the data will be lost if you'd try to save it somewhere else (e.g. onStop())
but: make sure, that this saving produces not too much overhead, since onPause() is called on several occasions (e.g. screen rotates...)