2016-11-16 98 views
0

首先,我知道我可以保留一個片段setRetainInstance(true);和檢索FragmentManagersavedInstanceState屏幕旋轉保留多個fragmens

但我的情況是,我有三個片段在我的活動,我「交換」使用交易,這取決於用戶的行爲。 我還可以重新創建當前片段,並在onSaveInstanceState(Bundle savedInstanceState)內保存標記以在用戶旋轉屏幕時恢復它。

我的問題來了,當用戶旋轉屏幕,在一個片段,然後單擊去前一個片段。由於我可以恢復的唯一片段是活動片段,因此我無法在不重新創建片段的情況下向前一個片段展示(丟失了所有的信息)。

這是我的代碼。此外,如果這不是一個好的溶劑,我會apreciate任何提示。

private Fragment1 fragment1; 
private Fragment2 fragment2; 
private Fragment3 fragment3; 
private String currentFragmentTAG; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_weight); 

    ... 

    mFragmentManager = getSupportFragmentManager(); 

    if (savedInstanceState != null) { 
     // HOW TO RECOVER ALL FRAGMENTS?? 
     fragment1 = ??; 
     fragment2 = ??; 
     fragment3 = ??; 

     String tag = savedInstanceState.getString(SAVED_FRAGMENT); 
     Fragment savedFragment = mFragmentManager.findFragmentByTag(tag); 
     replaceFragment(savedFragment, tag, null); 
    } 
} 

// Here, in other funcions, I initialize fragments and show one of them depending of user actions 
// I use replaceFragment function to "swap" fragments. 

private void replaceFragment(Fragment fragment, String tag, Map<String, Parcelable> objectsToBundle) { 
    if (fragment != null && !tag.equals(currentFragmentTAG)) { 
     FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction(); 

     if (objectsToBundle != null && !objectsToBundle.isEmpty()) { 
      Bundle bundle = new Bundle(); 

      for (Map.Entry<String, Parcelable> entry : objectsToBundle.entrySet()) { 
       bundle.putParcelable(entry.getKey(), entry.getValue()); 
      } 

      fragment.setArguments(bundle); 
     } 

     mFragmentTransaction.replace(R.id.fragment_weight_container, fragment, tag); 
     mFragmentTransaction.commit(); 

     currentFragmentTAG = tag; 
    } 
} 

在我的片段我用setRetainInstance(true)

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // retain this fragment 
    setRetainInstance(true); 
} 

太謝謝你了!

回答

0

該解決方案非常簡單。在我的replaceFragment函數中執行交易時,我只需要使用FragmentTransaction.addToBackStack(tag)。這樣,我在活動中使用的所有片段將是retaines,可以從FragmentManager

... 
FragmentTransaction mFragmentTransaction = mFragmentManager.beginTransaction(); 
mFragmentTransaction.addToBackStack(tag); 
... 

恢復所以這是我能恢復它們(如果已實例化)

if (savedInstanceState != null) { 
    mWeightMainFragment = (WeightMainFragment) mFragmentManager.findFragmentByTag(WeightMainFragment.TAG); 
    mWeightAddFragment = (WeightAddFragment) mFragmentManager.findFragmentByTag(WeightAddFragment.TAG); 
    mWeightChartFragment = (WeightChartFragment) mFragmentManager.findFragmentByTag(WeightChartFragment.TAG); 

    currentFragmentTAG = savedInstanceState.getString(SAVED_FRAGMENT); 
    Fragment savedFragment = mFragmentManager.findFragmentByTag(currentFragmentTAG); 
    replaceFragment(savedFragment, currentFragmentTAG, null); 
} 

獎勵方式:我還使用popBackStack()來撤銷transcactions,而不是用back棧片段再次替換。