Resetting preferences when using PreferenceScreen - java

I am loading a PreferenceScreen from an xml file to use as the screen to configure a new event so I'm attempting to clear and reset the values of the SharedPreference this activity is using. The problem I'm encountering is that when attempting to move to using a named preference manager, it seems the preference gets cleared but when I select an EditTextPreference element, the old data is still the default entered text on the popup.
In my onCreate method I'm attempting to initialize the preferences, clear them, then set to default values. My understanding from the dev resources were that there's no way to clear/reset in one step..
private static final String PREFNAME = "newmeetingactivity.preferences";
//load preferences and set name
addPreferencesFromResource(R.layout.newmeeting_preferences);
getPreferenceManager().setSharedPreferencesName(PREFNAME);
getPreferenceManager().setSharedPreferencesMode(MODE_PRIVATE);
//Clear the preferences
_sharedPreferences = getPreferenceManager().getSharedPreferences();
SharedPreferences.Editor ed = _sharedPreferences.edit();
ed.clear();
ed.commit();
//Load default preferences from file again
PreferenceManager.setDefaultValues(this, _sharedPreferences.toString() , MODE_PRIVATE, R.layout.newmeeting_preferences, true);
Edit: To try to better explain what I'm attempting to do (in case my approach is way off): I need to clear shared preferences used on a given activity while not interfering with the settings from other activities (as they should persist indefinitely).

Could you instead try using the PreferenceManager.getDefaultSharedPreferences(context) to get your prefs.
Edit:
adb shell into your application after you choose to reset the values. If you look at the preference file you will see that it's default value has been set. Try refreshing your activity. One way I did this was by simply killing it from the Applications menu. When the activity restarts it will have the expected default value.

Related

Shared Preferences getint not returning default value

I am using shared preferences and would like the default value to be 1 when there are no preferences found.
My code
int currentRadio;
...
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
currentRadio = prefs.getInt("radioSelected", 1);
I am unsure why I am not getting 1 for the default value.
To test I would uninstall the app from my phone then launch the app to my phone from android studio. Every time I get 2 as the default value. However, if I just clear the data from the app, I get 1 as the default value.
Any help is appreciated, thank you.
Just Go through this:
SharedPreferences sp = getSharedPreferences("your_prefs",Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();
Then you can get it as:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("your_int_key", -1);
The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API
Actually, you also need to provide a default value to getInt which is returned when an int with the key your_int_key could not be found. Something like this:
int myIntValue = sp.getInt("your_int_key", -1);
where -1 is the default value
Also, If you use API 9 or above you should use apply() instead of commit()
declare prefs like:
prefs = PreferenceManager.getDefaultSharedPreferences(context);
It would be happening due to you wrong implementation of shared preference.
Do one thing search for the key "radioSelected" in you app. if it found at any other place to save the value in shared preference, then make it comment.
Or just share your complete shared preference file.
If using android-21 the problem is not with preferences. Its with backup manager
Add in manifest file
android:allowBackup="false"

How to set app background via the SharedPreferences?

I want to allow users to change my app's background from the settings (for the Main_UI activity class). I am not able to store the uri in a string since there is no startActivityForResult() method that could extract the intent's uri (The Preferences only allow starting an intent, preferably to other settings)(I tried creating custom Preference Class but again, not startActivityForResult). I tried doing it via the menu options, taking the uri and creating a drawable from it. But when I apply that drawable to my RelativeLayout instance, it says its supported only beyond API 16. Mine is 15.
Any help would be really appreciated, since this android is getting on my nerves.
Use this approach to support lower version API(s):
if (Build.VERSION.SDK_INT > 15)
yourLayout.setBackground(yourDrawable);
else
yourLayout.setBackgroundDrawable(yourDrawable);
Using SharedPreferences
In the settings activity:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor edit = settings.edit();
edit.putString("bg_drawable_uri", bgDrawableUri);
edit.commit();
And in main activity:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
settings.getString("bg_drawable_uri", "default_uri");
I know this is very old and probably there might be better ways today, but what worked for me was to just create an ImageView, set the uri and then retrieve the drawable from the ImageView, like so:
ImageView imageView = new ImageView(context);
imageView.setImageURI(someUri);
getWindow().setBackgroundDrawable(imageView.getDrawable());

How to check if it is the first time a user has used my app

I am wondering what the best method is to check if it is the first time a certain user has used my app, and also when they update will it reset and make it think they are a new user.
Edit(For more precision): I want to store a user id number in my sql server, and I want every user to create a password when they first download, but I also want them to have the option to select "Already have account" so that they can sync their accounts across devices
Like Illegal Argument mentions, the most reliable way is to use a server, because if you use a Shared preference, the user can delete the data or move to a different device and appear to be a totally new user.
If you don't want to go through the trouble of creating and managing your own server, you might consider using Google's Cloud Save API. When your app starts, check for existing data in the cloud. If there's nothing there it's a new user so create the data. It should be as simple as that.
The most reliable way to know that the user is a first time user is via a server validation. Another way to do it would be to use shared preference and saving certain data in internal or external storage. However storing data in mobile is not so foolproof as data can be easily deleted by user.
Globals
String _StrCode="";
SharedPreferences preferences;
String _responseCode=null;
In oncreate but before set content view check
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
preferences = getSharedPreferences("mypref", 0);
_StrCode= preferences.getString("Code", _StrCode);
if(_StrCode.equalsIgnoreCase("")){
setContentView(R.layout.activity_qdc_status__launcher);
// here you can assign a value to
_StrCode ="some value";
Editor prefsEditor = preferences .edit();
prefsEditor.putString("Code",_StrCode);
prefsEditor.commit();
}else{
Intent _in= new Intent(firstscreen,secondscreen.class);
startActivity(_in);
finish();
}
}
i hope this will halp you and its easy to understand too.

How to implement a context menu for a <Preference> on android?

I have a preference stored in variable "preference":
Preference preference = new Preference(this);
I want to add a context menu to be displayed onlongclick. I have already created the menu /res/menu/pref_menu.xml and I tried registerForContextMenu(preference); but it only works on views. how can I implement it with a preference?
I solved it by register in onPostCreate method:
registerForContextMenu(getListView());
And set on the certain preference click listener when need to open:
openContextMenu(getListView());

How to secure information in Android

How can I keep secure data from crackers in an android application? I need to use some secret keys (as the application will work through a web service) so I need to know where it would be recommended to keep this secret information and how?
You could use SharedPreferences which is not stored in an accessible file/folder. Click here for information on SharedPreferences and here for how to use it. This is an example:
// Get settings
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
// Get editor to be able to put settings
SharedPreferences.Editor editor = settings.edit()
// Put information with specified key
editor.putString("example_key", "example value");
// Commit changes
editor.commit();
// Gets value from setting with the specified key
String exampleString = settings.getString("example_key", null);
You can save them in a Preferences object and encrypt them with a hard-coded key, save them to the app's data folder and encrypt the whole file with a hard-coded key, or save them in a web location (which is probably the safest way to do it). You could also substitute a user-provided password for a hard-coded key, but that may be a nuisance depending on what the app does. If you're asking for a place that Android provides specifically for storing sensitive info, I don't believe any exists.

Categories

Resources