如何将数据保存在片段开始另一片段后? [英] How to save data in Fragment after start another fragment?
问题描述
我有三个片段A,B,C
在片段A - 一个得到一些变量ID,从服务器然后创建包,把ID会开始B片段,在这个片段,我从包获取ID和显示由该ID上显示的所有信息,但是当我开始片段C然后我preSS按钮倒流,回到TI片段B,变量id为null。我该如何解决呢?我想我可以节省ID数据库中,但也许有更简单的方法?
我明白,当我preSS回片段C,B片段想从包获取信息,这就是为什么我做下一个:
捆绑包= getArguments();
如果(fakeID == NULL){
fakeID = bundle.getString(fakeID);
}
编辑:
公共无效onActivityCreated(捆绑savedInstanceState){
super.onActivityCreated(savedInstanceState);
如果(savedInstanceState == NULL)
{
捆绑包= getArguments();
fakeID = bundle.getString(fakeID);
}
其他{
fakeID = savedInstanceState.getString(fakeID);
}
}
@覆盖
公共无效的onSaveInstanceState(捆绑outState){
// TODO自动生成方法存根
super.onSaveInstanceState(outState);
outState.putString(fakeID,fakeID);
}
但是,当我回到这个片段
savedInstanceState == NULL在此声明
如果(savedInstanceState == NULL)
根据<一个href=\"http://stackoverflow.com/questions/15935322/fragmentactivity-onsaveinstancestate-not-getting-called\">this 的onSaveInstanceState
将不会被调用,除非活动本身已要求它。这意味着如果你在同一个活动变化的片段。 onSavedInstanceChanged
将不会被调用。因此,它是空。为纽带建议您可以选择使用共享preferences
或数据库
。
I have three fragments A,B,C In fragment A - a get some variable id, from server then i create bundle , put id in it and start fragment B, in this fragment i get id from bundle and display all information by this id on display, but when i start fragment C and then i press button back and return ti fragment B, variable id is null. How can i solve it? I think i can save id in Database,but maybe there is easier way? i understand that when i press back in fragment C, fragment B want to get information from bundle, thats why i make next:
Bundle bundle = getArguments();
if (fakeID == null) {
fakeID = bundle.getString("fakeID");
}
Edited:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState == null)
{
Bundle bundle = getArguments();
fakeID = bundle.getString("fakeID");
}
else{
fakeID = savedInstanceState.getString("fakeID");
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putString("fakeID", fakeID);
}
But when i return to this fragment savedInstanceState == null in this statement
if (savedInstanceState == null)
according to this onSaveInstanceState
won't be called unless the activity itself has called it. which means if you are in the same activity changing fragments. onSavedInstanceChanged
won't be called. Therefore it is null. As the link suggested you have to use either SharedPreferences
or Database
.
这篇关于如何将数据保存在片段开始另一片段后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!