My app have mainactivity
User must set a password and it saved in shared prefence like this
settings = getSharedPreferences("NAME", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("password", edpassstr);
editor.apply();
I can get this preference in another activity successfully
But I can't get it in the fragment
This is the code used to restore value from shared preference
settings = getActivity().getSharedPreferences("NAME", 0);
String passs = settings.getString("password", "");
The string passs is ="" inside fragment. Why?
How to get the string from shared preference in a fragment?
where are you calling getActivity() in your fragment? Because it will return null when it gets called before the fragment's onAttach method. In your fragment, try overriding the onAttach method, since it gets context as a parameter like so:
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
SharedPreferences settings = context.getSharedPreferences("NAME", 0);
}
If you want to use the shared preferences across the application. You should use applicationcontext instead of lifecycle owners (activity or fragments) context.
applicationcontext.getSharedPreferences(name, mode)
Declare as a global variable
SharedPreferences prefs;
SharedPreferences.Editor editor;
in onCreate() method declare as
prefs = getSharedPreferences("data", MODE_PRIVATE);
editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("password", "xyz_password");
editor.apply();
You can get the value from shared preference using this way
prefs.getString(key,default Value);
prefs.getString("password", "Any value if you not get any value");
It's so easy I hope it's helpful to you...!
Related
This is in my main activity class, I can pass and retrieve data in my other class through shared preferences but I cant clear the shared preferences in my other class. Also tell me how to check that my shared preferences are cleared.
SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "userKey";
public static final String Pass = "passKey";
sharedPreferences=getApplicationContext().getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name,userName);
editor.putString(Pass,password);
editor.commit();
this is in my other class
SharedPreferences sharedPreferences;
sharedPreferences=getApplicationContext().getSharedPreferences(SignUPActivity.MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
editor.commit();
Hi if you want just pass value from one Activity to Another or One Fragment to other than I suggest you to use INTENT to pass data
Look at this example how to pass -http://startandroid.ru/en/lessons/241-lesson-28-extras-passing-data-using-intent.html
Also -https://stackoverflow.com/a/30166602/4741746
sharedPreferences is used when data used for all activity which is permanatly stored in application you also can clear data
Remove given String from shearedPrefrance
public static void removeFromSharedPreferences(Context mContext, String key) {
if (mContext != null) {
SharedPreferences mSharedPreferences = mContext.getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, 0);
if (mSharedPreferences != null)
mSharedPreferences.edit().remove(key).commit();
}
}
Remove All value from SharedPrefrance
public static void removeSharedPreferences(Context mContext) {
if (mContext != null) {
SharedPreferences preferences = mContext.getSharedPreferences("PREFERENCE", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
}
}
And why your code is not working is you have to passs Context.MODE_PRIVATE insted MODE_PRIVATE jsut confirm both should have same value
Put this code in both the classes
SharedPreferences app_preferences = PreferenceManager
.getDefaultSharedPreferences(context);
Possible Reason is you are not using the same preferences.
In your first activity you are using:
sharedPreferences = getApplicationContext()
.getSharedPreferences(MyPREFERENCES,MODE_PRIVATE);
And in the other activity you should also use it.
Try to read these
http://developer.android.com/reference/android/app/Activity.html
also from the docs
Retrieve a SharedPreferences object for accessing preferences that are
private to this activity. This simply calls the underlying
getSharedPreferences(String, int) method by passing in this activity's
class name as the preferences name.
I have a fragment where I let set some SharedPreference values set.
In the fragment, everything works fine - I can get any value I want, saving, editing, deleting works fine.
Then I have an Activity, from where I want to get the value "savedValue1" - but it does not work
public static final String MyPref = "MyPreference";
static SharedPreferences sharedpreferences;
//onCreateView...
sharedpreferences = this.getActivity().getSharedPreferences(MyPref,
Context.MODE_PRIVATE);
editor.putString("savedValue1", someString);
editor.commit();
I tried it with in Fragment:
public static String getValue(){
return sharedpreferences.getString("savedValue1","");
}
in Activity:
String newValue = Fragment.getValue();
But that doesn't work - any hint?
You should not have a Fragment.getValue() method.
SharedPreferences are here to avoid that.
Use the same getSharedPreferences("whatever", Context.MODE_PRIVATE) code and you shall get/set the same values inside the same preferences.
That is how it is supposed to be used. From the official documentation:
For any particular set of preferences, there is a single instance of
this class that all clients share.
Use this code to save and retrieve values from SharedPreferences
//To save string
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor e = settings.edit();
e.putString("savedValue1", someString);
e.commit();
//Retrieve team score
String saved_value = settings.getString("savedValue1", "");
I'm using SharedPreferences to save username but it's not working.
In login Activity (read):
ocUserName = (EditText)findViewById(R.id.userNameText);
SharedPreferences prefs = this.getPreferences(Context.MODE_PRIVATE);
String userNameKey = "userName";
String userNameTV = prefs.getString(userNameKey,null);
SharedPreferences.Editor editor = prefs.edit();
if(userNameTV != null) {
ocUserName.setText(userNameTV);
}
in second Activity (write):
SharedPreferences prefs =
getSharedPreferences("com.mesbahsoft.IRIB", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
String userNameKey = "userName";
editor.putString(userNameKey, ocUser);
editor.commit();
Activity.getPreferences(int mode) (as used in your login activity) has a comment:
Retrieve a {#link SharedPreferences} object for accessing preferences that are private to this activity.
In your second activity you use Activity.getSharedPreferences(String name, int mode) and provide what looks like your Application Id as the name.
In effect, you are using two different sets of shared preferences in each activity.
I recommend using PreferenceManager.getDefaultSharedPreferences(Context context) if you intend to use the shared preferences throughout your application.
I assume the problem is that you are unable to read the written value. You need to use the same file in login activity:
SharedPreferences prefs = this.getSharedPreferences("com.mesbahsoft.IRIB", Context.MODE_PRIVATE);
If this does not work, kindly specify clearly where you are seeing the problem and also specify exceptions seen.
You are accessing different shared preferences in your activities,
this.getPreferences( Context.MODE_PRIVATE); is shorthand for this.getSharedPreferences( <activity's class name>, Context.MODE_PRIVATE);
In you login activity, use this,
getSharedPreferences("com.mesbahsoft.IRIB", MODE_PRIVATE);
References:
http://developer.android.com/reference/android/app/Activity.html#getPreferences(int)
http://developer.android.com/reference/android/content/Context.html#getSharedPreferences(java.lang.String,int)
I know this has been asked before, but i can't seem to figure it out
i am trying to get my preferences by this:
SharedPreferences preferences = this.getActivity().getSharedPreferences("storedName", Context.MODE_PRIVATE);
String loginemail = preferences.getString("storedName", "");
but this doesn't seem to work, i have multiple sharedPreferences which i need to get in my fragment what is the correct way to do it?
As getDefaulSharedPreferences(this) doesn't work.
i store my prefs like so:
savePreferences("shareUniqePass", uniqePassIds.getText().toString());
savePreferences("storedName", inputEmail.getText().toString());
savePreferences("Storedpass", inputPassword.getText().toString());
private void savePreferences(String key, boolean value){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor checkbox = sharedPreferences.edit();
checkbox.putBoolean(key, value);
checkbox.commit();
}
private void savePreferences(String key, String value){
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor names = sharedPreferences.edit();
names.putString(key, value);
names.commit();
}
Instead of getting it from getSharedPreferences use the getDefaultSharedPreferences.
As getDefaulSharedPreferences(this) doesn't work.
You used getDefaultSharedPreferences for saving the data and therefore you must use getDefaultSharedPreferences to get the data that was saved.
this mean the instance of your fragment instead use the getActivity() to get the instance of context from your activity.
sample:
String loginemail = PreferenceManager.getDefaultSharedPreferences(getActivity()).getString(PREF_USER_NAME, "");;
You need to make sure that your saving and loading the data from the same shared preferences. If you're only accessing it from the one activity / fragment, you should use getDefaulSharedPreferences(this).
If, however, you're going to use it from several different activities / fragments, you should use:
private void savePreferences(String key, boolean value){
SharedPreferences prefs = getActivity().getSharedPreferences("storedName", Context.MODE_PRIVATE);
Editor checkbox = sharedPreferences.edit();
checkbox.putBoolean(key, value);
checkbox.commit();
}
I am trying to save a date in one activity and then have that date put in a textView in another activity. I am not sure about how to get the two activities to communicate with each other.
In file called report.java I have this method that gets the date and save it in sharedPrefernces.
private void updateLabel() {
date.setText(fmtDate.format(dateAndTime.getTime()));
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("date", date.getText().toString()); // value to store
editor.commit();
}
I am trying to figure out how to get my file called inspection use this to populate a textView
The problem I think I am having is with getting the correct name for the report file.
public static final String PREF_FILE_NAME = "report";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
then I have this code on a method called onResume()
#Override
public void onResume() {
super.onResume();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String strDate=preferences.getString("date", date.getText().toString());
date.setText(strDate);
}
You are saving the value to two seperate preference files.
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
Use only one.
Why not use the default preference file that is accessible by all classes/activities of your app?
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(yourContext);
preferences.edit().putString(YOURKEY, yourStrValue);
This way you are not creating extra preference files in your app that you have to remember which values are stored in which files. Definately makes life easier.