1

I want to retrieve some values which ive saved to the sharedpreferences from one activity and want to call it in another.

Basically how can i get the name from the first activity and automatically display it in the field in the second activity?

So as an example i have this, which saves into the preferences.

public class Settings extends Activity {
    EditText editText;
    Button button;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);
        editText = (EditText) findViewById(R.id.editText);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new Button_Clicker());
        loadSavedPreferences();

    }
    private void loadSavedPreferences() {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        String name = sharedPreferences.getString("name", "Your Name");
        editText.setText(name);

    }

    private void savePreferences(String key, String value) {
        SharedPreferences sharedPreferences = PreferenceManager
                .getDefaultSharedPreferences(this);
        Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
    }

    class Button_Clicker implements Button.OnClickListener {
        public void onClick(View v) {

            savePreferences("name", editText.getText().toString());
            finish();
        }
    }

}

Now i have another activity where i can use the value instead of it being typed again:

public class Details extends Activity {

    EditText nameText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.details);
        nameText = (EditText) findViewById(R.id.nameText);


        Button button = (Button) findViewById(R.id.calculate);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                               }
        });
    }
user3411002
  • 783
  • 3
  • 9
  • 27
  • The use of SharedPreferences is already explained in this post: http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values – Robbe Jan 17 '15 at 00:02
  • See me answer in this link http://stackoverflow.com/questions/23137710/android-how-to-get-data-in-a-shared-preference-of-one-activity-to-another-acti/42943815#42943815 – Sunil Mar 22 '17 at 06:02

2 Answers2

5

One of the nice things about the SharedPreference class is that it is designed to be available to any Activity within you application. You can retrieve any SharedPreference with two simple calls:

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
String desiredPreference =  sharedPreferences.getString("NameOfSharedPreference", "DefaultValue");

While it may seem obvious make sure that you have actually created and stored the data you wish to save before you try and retrieve it. To continue on the example above, use the following code snippet to store your data into SharedPreferences:

Editor editor = sharedPreferences.edit();
editor.putString("NameOfSharedPreference", "ValueToStore");
editor.commit();
Willis
  • 5,308
  • 3
  • 32
  • 61
  • How do i make the value appear in a specific textbox without getting errors on the new activity. For example i have: nameText = (EditText) findViewById(R.id.nameText); How do i get the stored value to appear here? Thats where im getting confused. – user3411002 Jan 17 '15 at 00:02
  • You cannot retrieve a piece of data from `SharedPreferences` without having committed said piece of data first. I have amended my answer to reflect this... – Willis Jan 17 '15 at 00:13
  • Well im sure ive saved the data to the sharedpreference by looking at my code. Im able to retrieve it and edit it on that activity, but i want to retrieve and edit on a new activity on a new text field. When i do attempt to retrieve it my app crashes. – user3411002 Jan 17 '15 at 00:16
  • 1
    You should be asking "Why is my app crashing when I attempt to retrieve a value from SharedPreferences?" and always post your stacktrace when asking a question about a crash. – howettl Jan 17 '15 at 00:17
  • Post your stacktrace so that we can narrow down the issue – Willis Jan 17 '15 at 00:17
  • Dont worry guys, ive managed to solve the problem using the basic information Willis provided! Thanks! – user3411002 Jan 17 '15 at 00:18
1

You can call "PreferenceManager.getDefaultSharedPreferences(context)" from any activity of your apps.

I think you have an error in your code :put "editText.setText(weight)" instead of editText.setText(name);

said
  • 106
  • 5
  • Sorry about that, i was using old code for another program i made. Updated my post with that haha. My main question here is how do i show the data i retrieved in a text field, say i have an edittext field on another activity, how do i show the retrieved data onto it. – user3411002 Jan 17 '15 at 00:11