[英]Android - Saved Activity/Fragment State Never Clears
我有一个由Fragments组成的Android应用,可以正确保存状态。 问题是它工作得太好了;-)。 我将一些输入输入到几个EditText
元素中,然后通过“ onSaveInstanceState()”方法中的SharedPrefs保存它们,然后单击电话上的“任务管理器”或“切换应用程序”按钮(两个重叠的矩形为图标),然后向左滑动即可关闭我的应用程序。 如果然后转到App Drawer并重新运行该应用程序,则保存的输入将仍然存在。 我也在“ onDestroy()”方法中清除了已保存的实例状态,但是当从“任务管理器”“关闭”应用程序时(通过日志确认),显然没有调用该状态。
有什么建议吗? 我拥有的其他应用程序没有出现这种现象。 我希望在用户通过任务管理器关闭应用程序时以及可能在设定的时间后清除保存的输入。 对状态处理的标准实践有何想法?
我测试了一些我拥有的应用程序,并注意到默认的“联系人”应用程序实际上会保存一个新联系人,如果您开始一个新的联系人并在显式保存之前切换到另一个应用程序。 我想我可以做到,但我宁愿不这样做。
以下是特定片段的一些相关代码; 提前非常感谢您的协助。
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.v(Tag, "onSaveInstanceState()");
saveInstanceState();
}
@Override
public void onResume() {
super.onResume();
Log.v(Tag, "onResume()");
restoreInstanceState();
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(Tag, "onDestroy()");
clearInstanceState();
}
private void saveInstanceState() {
Log.v(Tag, "saveInstanceState()");
// get entered data
String name = mTxtName.getText().toString();
String notes = mTxtNotes.getText().toString();
// save data in Shared Prefs
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.putInt(KeyAmmunitionId, mAmmunitionId)
.putString(KeyName, name)
.putString(KeyNotes, notes)
.putString(StringUtils.CurrentFragmentKey, Tag)
.commit();
}
private void restoreInstanceState() {
Log.v(Tag, "restoreInstanceState()");
mTxtName = (EditText)getActivity().findViewById(R.id.frag_manage_ammunition_txtName);
mTxtNotes = (EditText)getActivity().findViewById(R.id.frag_manage_ammunition_txtNotes);
if (PreferenceManager.getDefaultSharedPreferences(mContext).contains(KeyName)) {
String ammunitionName = PreferenceManager.getDefaultSharedPreferences(mContext).getString(KeyName, StringUtils.EMPTY_STRING);
mTxtName.setText(ammunitionName);
}
if (PreferenceManager.getDefaultSharedPreferences(mContext).contains(KeyNotes)) {
String ammunitionNotes = PreferenceManager.getDefaultSharedPreferences(mContext).getString(KeyNotes, StringUtils.EMPTY_STRING);
mTxtNotes.setText(ammunitionNotes);
}
}
private void clearInstanceState() {
Log.v(Tag, "clearInstanceState()");
PreferenceManager.getDefaultSharedPreferences(mContext)
.edit()
.remove(KeyAmmunitionId)
.remove(KeyName)
.remove(KeyNotes)
.commit();
}
与使用SharedPrefs相比,我发现保留其状态的内部无头片段更容易实现/清除。
在您的片段类中,您拥有此类。
/**
* "Headless" Fragment that retains state information between
* configuration changes.
*/
public class RetainedFragment extends Fragment {
/**
* internal storage to be kept put here
*/
// assuming the only 'view' in the parent fragment is this editText with some string value.
String editTextValue;
public RetainedFragment(){
editTextValue = ""; //init values on first time in.
}
/**
* Hook method called when a new instance of Fragment is
* created.
*
* @param savedInstanceState
* object that contains saved state information.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Ensure the data survives runtime configuration changes.
setRetainInstance(true);
}
// have getter/setter methods
然后,在外部外观中,仅基于内部无头片段中存储的值来“创建” UI。
要么...
您可以使用我们弄清楚的RetainedFragmentManager。 一开始有点麻烦,可能有点令人困惑,但它的无头片段允许您以类似哈希图的方式存储Java对象,它们将在配置更改中持久存在,但是如果您的应用程序不存在完全关闭等
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.