I Used Shareprereference To Save The String In Textview, Afyer Turn Off App And Open Again, It Doesnt Work
As title, i want to get the new string after i edit the textView, after i edit it and turn on the app again, The textview were still empty, Could some one help me what did i miss i
Solution 1:
Just Create a class Session.java
publicclassSession {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
privatestatic final StringPREF_NAME = "SessionVariable";
publicstatic final StringKEY_ID = "id";
publicSession(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
publicvoidcreateLoginSession(String id) {
editor.putString(KEY_ID, id);
editor.commit();
}
publicHashMap<String, String> getUserDetails() {
HashMap<String, String> user = newHashMap<String, String>();
user.put(KEY_ID, pref.getString(KEY_ID, null));
return user;
}
}
put value into SharedPreference:
Session sm;
Stringid= ed1.getText().toString();
sm = newSession(MainActivity.this);
sm.createLoginSession(id);
Retrieve from SharedPreference:
Session sm;
sm = new Session(Welcome.this);
HashMap<String, String> user = sm.getUserDetails();
String id = user.get(Session.KEY_ID);
ed1.setText("Id = " + id);
Solution 2:
Answer based on guess.
When you create an new Activity you put data in SharedPreference.
For example in splash screen you add data to the SharedPereference for the first time. When the next time enter the same splash sceen and try to add some set of values in SharedPreference so maybe your value is empty or wrong while fetching next time.
In your case you are calling onStop()
in oncreate()
. So the value must empty.
Check the following. You must modify it. I just want to show why you are not getting any values.
publicclassMainActivityextendsActionBarActivity {
//private SharedPreferences saveUserName;private AutoCompleteTextView editText1, editText2, editText3,
editText4, editText5, editText6;
private ImageButton imageButton1, imageButton2, imageButton3,
imageButton4, imageButton5, imageButton6;
privatefinalStringPREFERENCES_NAME="userinfo";
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
getUserName();
SharedPreferencespreferences= getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
editText1.setText(preferences.getString("EditText1", null));
editText2.setText(preferences.getString("EditText2", null));
editText3.setText(preferences.getString("EditText3", null));
editText4.setText(preferences.getString("EditText4", null));
editText5.setText(preferences.getString("EditText5", null));
editText6.setText(preferences.getString("EditText6", null));
onStop();
}
privatevoidinitializeViews(){
editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
}
publicvoidonStop() {
super.onStop();
SharedPreferencessharePreferences= getSharedPreferences("userName", 0);
SharedPreferences.Editoreditor= sharePreferences.edit();
editor.putString("EditText1", editText1.getText().toString());
editor.putString("EditText2", editText2.getText().toString());
editor.putString("EditText3", editText3.getText().toString());
editor.putString("EditText4", editText4.getText().toString());
editor.putString("EditText5", editText5.getText().toString());
editor.putString("EditText6", editText6.getText().toString());
editor.commit();
}
publicvoidgetUserName()
{
SharedPreferencespreferences= getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
editText1.setText(preferences.getString("EditText1", null));
editText2.setText(preferences.getString("EditText2", null));
editText3.setText(preferences.getString("EditText3", null));
editText4.setText(preferences.getString("EditText4", null));
editText5.setText(preferences.getString("EditText5", null));
editText6.setText(preferences.getString("EditText6", null));
}
publicvoidgoToUserInfoActivity(View v)
{
Intentit=newIntent(this, UserInfoActivity.class);
startActivity(it);
}
}
Solution 3:
I think this line should be ;
SharedPreferencessharePreferences= getSharedPreferences(PREFERENCES_NAME, 0);
instead of;
SharedPreferencessharePreferences= getSharedPreferences("userName", 0);
Post a Comment for "I Used Shareprereference To Save The String In Textview, Afyer Turn Off App And Open Again, It Doesnt Work"