我有多个片段包含我想要保存的数据,并且能够从不同的片段进行访问。
如果我只想保存特定片段的状态,我该怎么做?
以下是我目前的导航方式:
片段“1”
case R.id.imageButton1:
Fragment fragment2;
fragment2 = new Frag2();
//fragment2.setArguments(args);
FragmentManager frgManager = getFragmentManager();
// frgManager.putFragment(bundle, key, fragment)
frgManager.beginTransaction().replace(R.id.content_frame, fragment2)
.commit();
break;
片段“2”
switch(v.getId()){
case R.id.imageButton2:
Fragment fragment;
fragment = new Frag1();
fragment.setArguments(fragment.getArguments());
android.app.FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
break;
case R.id.imageButton1:
fragment = new LifeFrag3();
android.app.FragmentManager frgManage = getFragmentManager();
frgManage.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
break;
我还尝试通过在哈希映射中存储值然后使用暂停和恢复来传递数据来传递数据,但我没有运气。 :/
答案 0 :(得分:1)
@Override
public void onCreate(Bundle savedInstanceState) {
...
if (savedInstanceState != null) {
mContent = getSupportFragmentManager().getFragment(
savedInstanceState, "mContent");
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
getSupportFragmentManager().putFragment(outState, "mContent", mContent);
}