0

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);

        }
    });
}
  • Possible duplicate of [How to use SharedPreferences in Android to store, fetch and edit values](http://stackoverflow.com/questions/3624280/how-to-use-sharedpreferences-in-android-to-store-fetch-and-edit-values) – Tim Oct 19 '15 at 11:57
  • With the samples from that link, save your data to prefs in activity 1, and retrieve it in mainactivity – Tim Oct 19 '15 at 11:57
  • share code.| if you are using saharedprefrences no need to pass it to another activity it is available to whole app – Gaurav Oct 19 '15 at 11:58
  • @TimCastelijns i think were not in a same situation – kim caboteja Oct 19 '15 at 12:06
  • Hard to tell. You haven't told us much about your situation – Tim Oct 19 '15 at 12:09
  • @GauravPolekar sorry, there's my code. – kim caboteja Oct 19 '15 at 12:36

2 Answers2

2

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();
Gaurav
  • 3,615
  • 2
  • 27
  • 50
0

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()).

Henry
  • 17,490
  • 7
  • 63
  • 98
  • 1
    *If you are going to save the data in SharedPreference then there is no point in passing it through Intent.* can't be sure about that. Depending on if you write prefs with `commit()` or `apply()`, the prefs might not be available yet when the new activity is being created – Tim Oct 19 '15 at 12:08
  • Yes. Totally forgot that. Thanks. – Henry Oct 19 '15 at 12:18