共享首选项即使在app close

时间:2015-10-19 11:55:23

标签: android

我有这个活动会使用意图将值传递给mainactivity并尝试烘烤它以查看它是否通过了值并且我实现了它!
意味着它的工作,我也使用sharedpreferences将这些值保存到textview。但当我关闭应用程序并再次尝试使用吐司检查它是否具有来自另一个活动的值而不使用意图时,toast显示任何内容。

有没有办法使用共享的首选项永久保存在mainactivity上?

修改

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

        }
    });
}

2 个答案:

答案 0 :(得分:2)

浪费了

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

答案 1 :(得分:0)

SharedPreference是全球性的。它不是特定于活动的。因此,一旦您从任何活动或片段中保存SharedPreference中的某些数据,它将在任何其他活动或片段中可用,即使您再次关闭并打开该应用程序也是如此。

如果您要将数据保存在SharedPreference中,那么将它传递给Intent是没有意义的(如果您使用commit()而不是apply())。