On My activity oncreate, I have defined the edittext and button to store the data on save button click, by using shared preference, but its not storing the data, whenever I get back to the same activity again, I don't understand why it's not storing. Here is my full code. While debugging I saw that its getting stored on this activity, after the finish of this activity the cache gets cleared.
public class MyActivity extends AppCompatActivity {
public EditText name1, name2, name3, name4, name5, name6, name7, name8, name9, name10, phoneName, phoneContact, contact1, contact2, contact3, contact4, contact5, contact6, contact7, contact8, contact9, contact10;
Button saveButton;
private Context context;
public SharedPreferences.Editor editorcache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sos);
context = this.getApplicationContext();
saveButton = (Button) findViewById(R.id.save);
name1 = ((EditText) findViewById(R.id.name1));
name2 = ((EditText) findViewById(R.id.name2));
name3 = ((EditText) findViewById(R.id.name3));
name4 = ((EditText) findViewById(R.id.name4));
name5 = ((EditText) findViewById(R.id.name5));
name6 = ((EditText) findViewById(R.id.name6));
name7 = ((EditText) findViewById(R.id.name7));
name8 = ((EditText) findViewById(R.id.name8));
name9 = ((EditText) findViewById(R.id.name9));
name10 = ((EditText) findViewById(R.id.name10));
contact1 = ((EditText) findViewById(R.id.contact1));
contact2 = ((EditText) findViewById(R.id.contact2));
contact3 = ((EditText) findViewById(R.id.contact3));
contact4 = ((EditText) findViewById(R.id.contact4));
contact5 = ((EditText) findViewById(R.id.contact5));
contact6 = ((EditText) findViewById(R.id.contact6));
contact7 = ((EditText) findViewById(R.id.contact7));
contact8 = ((EditText) findViewById(R.id.contact8));
contact9 = ((EditText) findViewById(R.id.contact9));
contact10 = ((EditText) findViewById(R.id.contact10));
phoneName = ((EditText) findViewById(R.id.phoneName));
phoneContact = ((EditText) findViewById(R.id.phoneContact));
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE);
editorcache = sharedPrefcache.edit();
editorcache.putString("name1", name1.getText().toString());
editorcache.putString("name2", name2.getText().toString());
editorcache.putString("name3", name3.getText().toString());
editorcache.putString("name4", name4.getText().toString());
editorcache.putString("name5", name5.getText().toString());
editorcache.putString("name6", name6.getText().toString());
editorcache.putString("name7", name7.getText().toString());
editorcache.putString("name8", name8.getText().toString());
editorcache.putString("name9", name9.getText().toString());
editorcache.putString("name10", name10.getText().toString());
editorcache.putString("contact1", contact1.getText().toString());
editorcache.putString("contact2", contact2.getText().toString());
editorcache.putString("contact3", contact3.getText().toString());
editorcache.putString("contact4", contact4.getText().toString());
editorcache.putString("contact5", contact5.getText().toString());
editorcache.putString("contact6", contact6.getText().toString());
editorcache.putString("contact7", contact7.getText().toString());
editorcache.putString("contact8", contact8.getText().toString());
editorcache.putString("contact9", contact9.getText().toString());
editorcache.putString("contact10",contact10.getText().toString());
editorcache.putString("phoneName",phoneName.getText().toString());
editorcache.putString("phoneContact",phoneContact.getText().toString());
editorcache.commit();
finish();
}
});
}
}