我想將嵌套的子片段添加到父片段中。編程將片段添加到LinearLayout
所有工作正常,但....
起初,我的代碼:
public class FragmentDatasheetWithHeader extends Fragment {
private long mRowId;
private String mSid;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
final ScrollView scrollView = new ScrollView(getActivity());
final LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setId(4711);
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
createUI();
return scrollView;
}
private void createUI() {
final FragmentProductDetailHeader fragmentHeader = FragmentProductDetailHeader.newInstance(this.mRowId, FragmentProductDetailHeader.HEADERTYPE_SMALL);
final FragmentDatasheet fragmentDatasheet = FragmentDatasheet.newInstance(this.mRowId, this.mSid);
final FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().add(4711, fragmentHeader, "fragmentHeader").commit();
fragmentManager.beginTransaction().add(4711, fragmentDatasheet, "fragmentDatasheet").commit();
}
}
現在我的問題:
在我啓動「開發者選項不保留活動(銷燬每當用戶離開它時的活動)「
當FragmentDatasheetWithHeader的活動打開並且應用程序進入後臺並返回前臺時,嵌套ed碎片增加了一倍。但只有嵌套Fragments的容器是LinearLayout纔會出現。如果容器是一個FrameLayout,嵌套的Fragments不會加倍。
怎麼回事?
嘗試用替換替換添加。 – Leandros
它解決了我的問題,但現在我只看到最後一個碎片不是兩個:-( –