Android:在主活动容器中用其父片段替换子片段

时间:2018-12-03 12:31:44

标签: android android-fragments

想象一下我的主要活动有Viewpager。我有2个称为(F1 & F2)的片段,它们将交易到viewpager中。

再次假设在F1片段中我想设置一个按钮。单击按钮时,我想交易其他片段呼叫SUBF1,但不交易成F1片段。

我的问题在这里!!!我的主意是我想用其父片段替换子片段,该父片段保留在主要活动的片段容器中吗?

2 个答案:

答案 0 :(得分:0)

final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(R.id.details, new NewFragmentToReplace(), "NewFragmentTag"); 
ft.commit(); 

答案 1 :(得分:0)

You can save the instance of current fragment, when you are navigating from one fragment to other. When user press the back button, you can open the specific fragment with the help of tag.

 @Override
    public void onBackPressed() {

 String currentTag = currentFragment.getTag();
if(currentTag.equals(res.getString(R.string.fragmentTagTest)))
            {
                currentFragment = AnotherFragment;
                replaceFragment() // integrate replace current fragment with new one. 
            }
}