android 当在第二个片段中不使用addToBackStack时,第三个片段在backPress上覆盖第一个片段

oknrviil  于 2023-04-18  发布在  Android
关注(0)|答案(1)|浏览(108)

我打开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并单击显示第三个片段+第一个片段。

k3bvogb1

k3bvogb11#

试试这个

MainActivity.kt

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    binding = ActivityMainBinding.inflate(layoutInflater)
    setContentView(binding.root)
    loadFragment(FirstFragment())    // add this line
}

private fun loadFragment(fragment: Fragment) {
    val fragmentManager = supportFragmentManager
    val fragmentTransaction = fragmentManager.beginTransaction()
    fragmentTransaction.replace(R.id.frameLayout, fragment)
    fragmentManager.popBackStack()
    fragmentTransaction.commit()
}

FirstFragment.kt

binding.btnNext.setOnClickListener {
        val fragmentManager = fragmentManager
        val fragmentTransaction = fragmentManager?.beginTransaction()
        fragmentTransaction?.add(R.id.frameLayout, SecondFragment(), getString(R.string.fragment_tag_title_second_fragment))
        fragmentTransaction?.addToBackStack("Second")
        fragmentTransaction?.commitAllowingStateLoss()
    }

SecondFragment.kt

private fun onClick() {
    binding.apply {
        btnNext.setOnClickListener { loadFragment(ThirdFragment()) }
        btnPrevious.setOnClickListener {   activity?.onBackPressed() }
    }
}

private fun loadFragment(fragment: Fragment) {
    val fragmentTransaction = fragmentManager?.beginTransaction()
    fragmentTransaction?.add(R.id.frameLayout, fragment,getString(R.string.fragment_tag_title_third_fragment))
    fragmentTransaction?.addToBackStack("third")
    fragmentTransaction?.commitAllowingStateLoss()
}

ThirdFragment.kt

private fun onClick() {
    binding.apply {
        btnPrevious.setOnClickListener {
            activity?.onBackPressed()
        }
    }
}

相关问题