【发布时间】:2014-06-08 22:32:17
【问题描述】:
我有两个活动,其中主要活动有一个 TextView,可以在第二个活动的 sharedPreference 中进行更改。当用户想要更改或“保存”字符串时,它会将其保存在 SP 文件中,然后返回到主活动。但是,TextView 不会更改为新的并显示旧的。该应用需要重新启动才能运行。
我的目标是在系统 onResume 上使用活动生命周期,但这根本没有拾取新字符串。
我在问如何在保存/从第二个活动返回时更改 TextView。 有问题的行是: checkboxmessage = (sp.getString("checkboxdescr", ""));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
//
// sp= PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); // forget about
sp = getSharedPreferences("contactapp", 0);
// named preferences - get the default ones and finish with it
//SET TEXTS
smsintroduction = (sp.getString("intro", ""));
smsbody = (sp.getString("body", ""));
checkboxtext = (sp.getString("checkbody", ""));
checkboxmessage = (sp.getString("checkboxdescr", ""));
TextView tv = (TextView) findViewById(R.id.sexycheckbox);
tv.setText(checkboxtext);
CheckBox cb = (CheckBox) findViewById(R.id.sexycheckbox);
////TOPCLEAR
TextView tt = (TextView)findViewById(R.id.toptext2);
tt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText et = (EditText)findViewById(R.id.edit_name);
EditText et2 = (EditText)findViewById(R.id.edit_number);
et.setText("");
et2.setText("");
CheckBox sexycheck;
sexycheck = (CheckBox)findViewById(R.id.sexycheckbox);
if (sexycheck.isChecked()) {
sexycheck.setChecked(false);
}
}
});
}
protected void onResume(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
sp = getSharedPreferences("contactapp", 0);
smsintroduction = (sp.getString("intro", ""));
smsbody = (sp.getString("body", ""));
checkboxtext = (sp.getString("checkbody", ""));
checkboxmessage = (sp.getString("checkboxdescr", ""));
TextView tv1 = (TextView) findViewById(R.id.sexycheckbox);
tv1.setText(checkboxtext);
}
【问题讨论】:
标签: android android-intent sharedpreferences android-lifecycle