1

I created shared preferences activity, and i have 2 strings saved...

    public class sharedprefs extends Activity {


    EditText email;
    EditText lozinka;
    Button btnEmail;
    Button btnLozinka;
    Button btnPovratak;
    TextView email2; 
    TextView lozinka2;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sharedpref);

        email2 = (TextView)findViewById(R.id.textView4);
        lozinka2 = (TextView)findViewById(R.id.textView5);

        email = (EditText)findViewById(R.id.editText1);
        lozinka = (EditText)findViewById(R.id.editText2);

        btnEmail = (Button) findViewById(R.id.button1);
        btnLozinka = (Button) findViewById(R.id.button2);
        btnPovratak = (Button) findViewById(R.id.button3);

        LoadPreferences();

        btnEmail.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                SavePreferences("EMAIL", email.getText().toString());
                LoadPreferences();  


            }
        });

        btnLozinka.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                SavePreferences("LOZINKA", lozinka.getText().toString());
                LoadPreferences();


            }
        });

        btnPovratak.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {   

                finish();


            }
        });
    }

    private void SavePreferences(String key, String value){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.commit();
       }

       private void LoadPreferences(){
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String stremail = sharedPreferences.getString("EMAIL", "");
        String strlozinka = sharedPreferences.getString("LOZINKA", "");
        email2.setText(stremail);
        lozinka2.setText(strlozinka);
       }


}

I tested it and displayed them with textview... When i exit and re-enter my app, they are still displayed. Now i need this two strings for use with httpClient in my main activity. Problem is, I don't know how to load them in my main activity, and what are things I need to do(declare in main activity) for this to work ??

Goran
  • 1,239
  • 4
  • 23
  • 36

2 Answers2

3

Check out the instructions on the Android Developers documentation for handling SharedPreferences across multiple activities. This has also been covered in another answer.

The recommended way to access SharedPreferences is:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

so in your case, you could write it as:

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);

This will work both in a PreferenceActivity and in your normal main Activity.

Community
  • 1
  • 1
David
  • 1,698
  • 16
  • 27
  • Ok, thank you. End in which part of activity do I call it?? Doesn't mater or ?? And if you please can tell me, what else do i nned to define in main activity, and how do I transfer this saved preferences into te string ?? Or how do I use them in main activity ? – Goran Sep 27 '12 at 06:47
  • You could simply copy the two methods you used in the PreferenceActivity into your main Activity. Just make sure you assign the SharedPreferences object before you try to access it! It might help to define your "preferences" variable at the top of your class, to share it between multiple functions within the class. – David Sep 27 '12 at 23:16
2

You need to call the same method LoadPreferences() you need to code in your Main Activity ( or in any other activity where you want this SharedPreference.

private void LoadPreferences()
{
        SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
        String stremail = sharedPreferences.getString("EMAIL", "");
        String strlozinka = sharedPreferences.getString("LOZINKA", "");
        // Strings variable are ready with the values, you can assign them to other component if you want
}
Lucifer
  • 29,392
  • 25
  • 90
  • 143