MarcusRey
MarcusRey

Reputation: 383

Retrieving sharedpreferences string in another activity

I am trying to save a few edittext fields in one activity using sharedpreferences and then output those saved values to textview fields on another activity. The data appears to be saving ok in the first activity but I am not able to resolve the stored variable in the new activity. Does anyone know what i'm doing wrong? Thanks!

public class RelativeLayout extends AppCompatActivity {

EditText edit1;
EditText edit2;
EditText edit3;
public String Name = "nameKey";
public String Email = "emailKey";
public String Phone = "phoneKey";
public static final String MyPREFERENCES = "PrefStore" ;
SharedPreferences settings;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_relative_layout);
    final Button ButtonReturn = (Button) findViewById(R.id.button3);
    settings = getSharedPreferences(MyPREFERENCES, 0);
    //Onclick listen for return button press. On press App returns to main      activity.
    ButtonReturn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            edit1=(EditText)findViewById(R.id.editText);
            edit2=(EditText)findViewById(R.id.editText2);
            edit3=(EditText)findViewById(R.id.editText3);
            String n  = edit1.getText().toString();
            String e  = edit2.getText().toString();
            String p  = edit3.getText().toString();
            SharedPreferences.Editor editor = settings.edit();
            editor.putString(Name, n);
            editor.putString(Email, e);
            editor.putString(Phone, p);
            editor.apply();

The second activity is below. I am trying to retrieve the Name variable in the sharedpreferences but it appears in red with an error. Cannot resolve symbol 'Name'.

public class MainActivity extends AppCompatActivity {

SharedPreferences settings;
public static final String MyPREFERENCES = "PrefStore" ;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button ButtonDCUWeb = (Button) findViewById(R.id.ButtonDCUWeb);
    final Button ButtonStartCamera = (Button) findViewById(R.id.ButtonStartCamera);
    final Button ButtonLinearLayout = (Button) findViewById(R.id.ButtonLinearLayout);
    final Button ButtonRelativeLayout = (Button) findViewById(R.id.ButtonRelativeLayout);
    settings = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
    String value = settings.getString(Name, "Default_Value");

Upvotes: 0

Views: 53

Answers (3)

Mohammed Aouf Zouag
Mohammed Aouf Zouag

Reputation: 17132

in your RelativeLayout activity, put this in your onClick callback :

public class RelativeLayout extends AppCompatActivity {
    ...
    ...
    public static String Name = "nameKey";
    public static String Email = "emailKey";
    public static String Phone = "phoneKey";

    ...
    ...

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(Name, n);
    editor.putString(Email, e);
    editor.putString(Phone, p);
    editor.apply();
}

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ...
    ...

    SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
    String nameValue = sharedPreferences.getString(RelativeLayout.Name, "Default_Name_Value");
    String emailValue = sharedPreferences.getString(RelativeLayout.Email, "Default_Email_Value");
    String phoneValue = sharedPreferences.getString(RelativeLayout.Phone, "Default_Phone_Value");
    // Do something with the values you retrieve
}

You do not need to hold a reference to your sharedPreferences inside your activities during the process.

Upvotes: 0

Hussein El Feky
Hussein El Feky

Reputation: 6707

You need to declare Name before onCreate() in your second activity too to be able to reference it. Add:

public String Name = "nameKey";

Upvotes: 0

Mateus Brandao
Mateus Brandao

Reputation: 900

Your variable Name is only defined on the scope of your first activity. You need to make it accessible in the second activity... You can do two things:

1 - Declare a variable Name in your second activity with the same value as the first. 2 - Make the Name variable static so you can access it in the second activity: public static String Name = "nameKey"; And in your second activity, you can access it this way:

String value = settings.getString(RelativeLayout.Name, "Default_Value"); 

Upvotes: 1

Related Questions