Android-保存的活动/片段状态从不清除
我有一个Android应用程序,由片段组成,我正确保存状态。问题是它工作得有点太好了。我将在几个Android-保存的活动/片段状态从不清除,android,android-fragments,sharedpreferences,android-lifecycle,Android,Android Fragments,Sharedpreferences,Android Lifecycle,我有一个Android应用程序,由片段组成,我正确保存状态。问题是它工作得有点太好了。我将在几个EditText元素中输入一些输入,然后通过“onSaveInstanceState()”方法中的SharedPrefs保存这些元素,然后点击手机上的“任务管理器”或“切换应用程序”按钮(有两个重叠的矩形作为图标),然后向左滑动以关闭我的应用程序。如果我随后转到应用程序抽屉并重新运行应用程序,则保存的输入仍将存在。我也在清除“ondestory()”方法中保存的实例状态,但显然在从该任务管理器“关闭”
EditText
元素中输入一些输入,然后通过“onSaveInstanceState()”方法中的SharedPrefs保存这些元素,然后点击手机上的“任务管理器”或“切换应用程序”按钮(有两个重叠的矩形作为图标),然后向左滑动以关闭我的应用程序。如果我随后转到应用程序抽屉并重新运行应用程序,则保存的输入仍将存在。我也在清除“ondestory()”方法中保存的实例状态,但显然在从该任务管理器“关闭”应用程序时(通过日志记录确认)不会调用该状态
有什么建议吗?我拥有的其他应用程序没有表现出这种行为。我希望在用户通过任务管理器关闭应用程序时,以及可能在设定的时间后,清除保存的输入关于国家处理的标准做法有什么想法吗?
我测试了一些应用程序,注意到默认的联系人应用程序实际上会保存一个新联系人,如果你在显式保存之前启动一个新联系人并切换到另一个应用程序。我想我可以这样做,但我不想
下面是特定片段的一些相关代码;事先非常感谢您的帮助
@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();
}
我发现保留其状态的内部无头片段比使用SharedPref更容易实现/清理 在fragment类中,您有这个类
/**
* "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。它有点不稳定,一开始可能会有点困惑,但它是一个无头片段,允许您以类似hashmap的方式存储java对象,并且它们将在配置更改期间保持不变,但如果您的应用程序完全关闭,它们将不存在,等等