该值输入到活动A的编辑文本中。如果不保存又返回A而转到另一个活动B,则活动A的编辑文本中的表单值将被清除。如何还原编辑文本值?
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("company", company.getText().toString());
outState.putString("name", name.getText().toString());
outState.putString("email",email.getText().toString());
outState.putString("phone",phone.getText().toString());
outState.putString("address",address.getText().toString());
outState.putString("desc",desc.getText().toString());
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
co = savedInstanceState.getString("comoany");
em = savedInstanceState.getString("email");
ph = savedInstanceState.getString("phone");
add = savedInstanceState.getString("address");
na = savedInstanceState.getString("name");
de = savedInstanceState.getString("desc");
}
@Override
protected void onResume() {
Log.i("this", "Company::::::::::::" + co);
company.setText(co);
super.onResume();
}
最佳答案
您需要重写活动A中的onSaveInstanceState并按如下所示存储编辑文本的值:
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString(TEXT_VIEW_KEY, mTextView.getText());
// call superclass to save any view hierarchy
super.onSaveInstanceState(outState);
}
并且在您的onCreate活动中现在应该检查以前是否保存过任何数据,然后初始化您的textview
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// recovering the instance state
if (savedInstanceState != null) {
String text = savedInstanceState.getString(TEXT_VIEW_KEY,);
}
setContentView(R.layout.main_activity);
// initialize member TextView so we can manipulate it later
mTextView = (TextView) findViewById(R.id.text_view);
mTextView.setText(text);
}
您可能还需要检查android的体系结构组件,特别是生命周期感知组件,以减轻您的工作时间
关于android - 恢复输入的编辑文本值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51322048/