0

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();
            }
        });
     }
}
Ergin Ersoy
  • 890
  • 8
  • 28

2 Answers2

0

Move these code to outside onclick method then i think you will get those details

SharedPreferences sharedPrefcache = context.getSharedPreferences("Details", Context.MODE_PRIVATE);
editorcache = sharedPrefcache.edit();
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Ritu Suman Mohanty
  • 734
  • 1
  • 6
  • 15
0

Just check below answer

store data in SharedPreferences

SharedPreferences sharedPrefcache = 
MainActivity.this.getSharedPreferences("Details", Context.MODE_PRIVATE);
SharedPreferences.Editor editorcache = sharedPrefcache.edit();
editorcache.putString("id", "123");
editorcache.putString("name", "nikhil");
editorcache.commit();
finish();

fetch data from SharedPreferences

String id = sharedPrefcache.getString("id","");
String name = sharedPrefcache.getString("name","");
Nikhil Lotke
  • 605
  • 7
  • 15