in activity1 (emailpreferences), I take the email through an edittext. I would like to use this Email again on reopenning application
This is my code:
public static final String MyPREFERENCES = "MyPref";
public static final String Email = "emailkey";
SharedPreferences sharedPreferences;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.emailpreferences);
edit1=(EditText)findViewById(R.id.editText);
buttonpref=(Button)findViewById(R.id.button);
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
buttonpref.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = edit1.getText().toString();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Email, email);
But the email is not retrieved when the following is done:
public class MainActivity extends AppCompatActivity {
Text name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
String name = sharedPreferences.getString(Email, "email");
Toast.makeText(this,name, Toast.LENGTH_LONG).show();
}
}