我打开fragments
如下:[1]+有addToBackStack
)〉[2](无addToBackStack
)〉[3]
在Fragment 3
中,当单击onBackPress
按钮时,2nd
不可见,但3rd
覆盖1
.
如果我在2nd fragment
中也使用addToBackStack
,则一切正常(没有覆盖问题)。
但是我必须在用户访问3rd fragment
时隐藏2nd fragment
,并直接返回1st fragment
。
下面是我的代码:
从activity
使用fragmentTransaction.add
打开root fragment
[1]
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.add(R.id.abbcHomeFragmentContainer, new FragmentHomeAbbc(), null);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
[1](带addToBackStack
)〉[2]:开口2nd fragment
FragmentListOfBankAbbc fragment= new FragmentListOfBankAbbc();
fragment.setArguments(bundle);
if (getActivity()!=null){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Toast.makeText(getContext(), "Something went wrong (#201)", Toast.LENGTH_SHORT).show();
}
[2](不含addToBackStack
)〉[3]
private void funOpenFragments(Fragment fragment, Bundle bundle){
fragment.setArguments(bundle);
if (getActivity()!=null){
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.abbcHomeFragmentContainer, fragment, null);
//fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
Toast.makeText(getContext(), "Something went wrong (#202)", Toast.LENGTH_SHORT).show();
}
}
现在打开3rd fragment
并单击显示第三个片段+第一个片段。
1条答案
按热度按时间k3bvogb11#
试试这个
MainActivity.kt
FirstFragment.kt
SecondFragment.kt
ThirdFragment.kt