kim caboteja
kim caboteja

Reputation: 55

Shared Preferences passed to another activity permanently even on app close

I have this activity that will pass the values using intent to the mainactivity and tried to toast it to see if it passed the values and i achieved it!
Means its working and also i save those values to textview using sharedpreferences. but when i close the app and try using toast again to check if it has the values from the another activity without using intent the toast displays nothing.

Is there any way to save it on mainactivity permanently using also the shared prefs?

EDIT

MainActivity

SA prefcontacts;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent intent = getIntent();
    final String prefcon1 = intent.getStringExtra("prefcon1");
    final String prefcon2 = intent.getStringExtra("prefcon2");
    final String prefcon3 = intent.getStringExtra("prefcon3");
    final String prefcon4 = intent.getStringExtra("prefcon4");
    final String prefcon5 = intent.getStringExtra("prefcon5");      

    Button show = (Button)findViewById(R.id.button3);

    show.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v)
        {
            Toast.makeText(getApplicationContext(), prefcon1, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), prefcon2, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), prefcon3, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), prefcon4, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), prefcon5, Toast.LENGTH_SHORT).show();
        }
    });
}

SA.class

    EditText et1, et2, et3, et4, et5;

Button btnSave;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_s);
    btnSave = (Button)findViewById(R.id.btnSave);



    et1 = (EditText)findViewById(R.id.editText1);
    et2 = (EditText)findViewById(R.id.editText2);
    et3 = (EditText)findViewById(R.id.editText3);
    et4 = (EditText)findViewById(R.id.editText4);
    et5 = (EditText)findViewById(R.id.editText5);

    SharedPreferences settings = getSharedPreferences("MY_PREFS", 0);
    et1.setText(settings.getString("prefcon1", ""));
    et2.setText(settings.getString("prefcon2", ""));
    et3.setText(settings.getString("prefcon3", ""));
    et4.setText(settings.getString("prefcon4", ""));
    et5.setText(settings.getString("prefcon5", ""));

    btnSave.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
            Toast.makeText(getApplicationContext(), "Preferences Saved!", Toast.LENGTH_LONG).show();
            Intent intent = new Intent (SA.this, MainActivity.class);
            intent.putExtra("prefcon1", et1.getText().toString());
            intent.putExtra("prefcon2", et2.getText().toString());
            intent.putExtra("prefcon3", et3.getText().toString());
            intent.putExtra("prefcon4", et4.getText().toString());
            intent.putExtra("prefcon5", et5.getText().toString());
            startActivity(intent);

        }
    });
}

Upvotes: 0

Views: 66

Answers (2)

Gaurav
Gaurav

Reputation: 3763

isted of

intent.putExtra("prefcon1", et1.getText().toString());
            intent.putExtra("prefcon2", et2.getText().toString());
            intent.putExtra("prefcon3", et3.getText().toString());
            intent.putExtra("prefcon4", et4.getText().toString());
            intent.putExtra("prefcon5", et5.getText().toString());

//PUT

SharedPreferences.Editor editor = s.edit();
        editor.putString();
        editor.commit("prefcon2", et2.getText().toString());
        editor.commit("prefcon3", et3.getText().toString());
        editor.commit("prefcon4", et4.getText().toString());
        editor.commit("prefcon5", et5.getText().toString());
editor.commit();

Upvotes: 2

Henry
Henry

Reputation: 17851

SharedPreference is global. It's not activity specific. So once you save some data in the SharedPreference from any Activity or Fragment, it will be available in any other Activity or Fragment, even when you close and open the app again.

If you are going to save the data in SharedPreference then there is no point in passing it through Intent (if you use commit() rather than apply()).

Upvotes: 0

Related Questions