【发布时间】:2016-01-18 12:06:33
【问题描述】:
嗨,当一个片段被另一个片段替换时,我正在尝试保存片段的状态。 考虑在我的活动中,我有一个带有 textview-1 和 textview-2 的 fragmentA。当我点击 textview-1 时,我 用具有列表视图的 fragmentB 替换 fragmentA。 现在,当我单击 fragmentB 列表项时,我正在获取列表值并更新 textview-1。我正在为 textview-2 做同样的事情,但是当我返回时, textview-1 的值消失了。如何保存片段 A 的状态及其 textview-1 值。
尝试如下
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("curChoice", strtext);
}
在 OnActivityCreated() 中
if (savedInstanceState != null) {
// Restore last state for checked position.
strtext = savedInstanceState.getString("curChoice", "");
//incrementdata(strtext);
}
但 savedInstanceState 总是给出 null。
【问题讨论】:
-
将值保存在
cache或sharedpref中,并在需要时检索它。 -
你可以尝试在 onCreateView() 中拯救你的 savedInstanceState
-
@hari86:查看此链接developer.android.com/intl/ru/guide/components/…
-
你找到解决办法了吗?