Reputation: 1239
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 ??
Upvotes: 1
Views: 12995
Reputation: 1718
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.
Upvotes: 3
Reputation: 29632
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
}
Upvotes: 2