Reputation: 61
I am very new to this and hate to add so much code but I've spent so much time on this I am out of choices. I have an activity/page that saves some userdetails. Now, I am trying to save these details so that next time when they open the activity/page the data shows up.
I've tried doing this by starting a loadSavedPreferences method in the oncreate method. However, this is not working and if I go back to the frontpage and reenter this activity it's all gone again. Answers in code would be greatly appreciated.
JAVA:
public class my_settings extends Activity {
EditText name;
EditText email;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_settings);
name= (EditText) findViewById(R.id.name);
email= (EditText) findViewById(R.id.email);
loadSavedPreferences();
}
public void save(View view)
{
SharedPreferences userDetails = getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=userDetails.edit();
editor.putString("storedName",name.getText().toString());
editor.putString("storedEmail",email.getText().toString());
editor.commit();
Toast.makeText(this,"Data was saved succesfully",Toast.LENGTH_LONG).show();
}
private void loadSavedPreferences() {
SharedPreferences userDetails = referenceManager.getDefaultSharedPreferences(this);
String tempName = userDetails.getString("storedName", "Name"); // (key, default)
String tempEmail = userDetails.getString("storedEmail","Email Address");
name.setText(tempName);
email.setText(tempEmail);
}
}
The XML code looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.jesper.im_ok.my_settings"
android:clickable="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please enter contact details here:"
android:id="@+id/settings_title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="20sp"
android:clickable="false"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name of Contact"
android:ems="10"
android:id="@+id/name"
android:layout_below="@+id/settings_title"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:text="Email Address"
android:ems="10"
android:id="@+id/email"
android:layout_below="@+id/name"
android:layout_alignLeft="@+id/name"
android:layout_alignStart="@+id/name" />
</RelativeLayout>
Upvotes: 0
Views: 2640
Reputation: 609
To put values in your shared preferences you need call the method edit "pref.edit().putString..."
public class MainActivity extends Activity {
private SharedPreferences pref = null;
private final String SETTINGS_PREF_NAME = "UserDetails";
EditText email;
EditText name;
public void save( final View view ) {
this.pref = this.getSharedPreferences( this.SETTINGS_PREF_NAME, Context.MODE_MULTI_PROCESS );
this.pref.edit().putString( "storedName", this.name.getText().toString() ).commit();
this.pref.edit().putString( "storedEmail", this.email.getText().toString() ).commit();
Toast.makeText( this, "Data was saved succesfully", Toast.LENGTH_LONG ).show();
}
@Override
protected void onCreate( final Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
this.setContentView( R.layout.activity_main );
this.loadSavedPreferences();
}
private void loadSavedPreferences() {
final String tempName = this.pref.getString( "storedName", "Name" ); // (key, default)
final String tempEmail = this.pref.getString( "storedEmail", "Email Address" );
this.name.setText( tempName );
this.email.setText( tempEmail );
}
}
Upvotes: 0
Reputation: 695
In save method, you are using getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
which is user created SharedPreferences table and
While accessing the data in you are trying to get values from defaultSharedPreferences which is default one provided by android. This is wrong.
try this in loadSavedPreferences
SharedPreferences userDetails = this.getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
Hope this helps!!
Upvotes: 1
Reputation: 1167
private void loadSavedPreferences() {
SharedPreferences userDetails = this.getSharedPreferences("UserDetails", Context.MODE_PRIVATE);
String tempName = userDetails.getString("storedName", "Name"); // (key, default)
String tempEmail = userDetails.getString("storedEmail","Email Address");
name.setText(tempName);
email.setText(tempEmail);
}
test this
Upvotes: 0