3rd Fragment 在不使用 addToBackStack 的情况下,会覆盖 1st Fragment 的返回操作。

huangapple go评论65阅读模式
英文:

3rd Fragment override 1st on backPress, when don't use addToBackStack in 2nd fragment

问题

I open fragments like this: [1]+with addToBackStack) > [2](without addToBackStack) > [3]

在打开 fragment 时我这样做:[1](使用 addToBackStack) > [2](不使用 addToBackStack) > [3]

in Fragment 3 when click onBackPress Button then 2nd is not visible but 3rd override 1.
Fragment 3 中点击 onBackPress 按钮时,2nd 不可见,但 3rd 覆盖了 1

If I use addToBackStack in 2nd fragment also, then everything is fine(No override issue).
如果我在 2nd fragment 中也使用 addToBackStack,那么一切都正常(没有覆盖问题)。

> But I have to hide 2nd fragment when user visit 3rd fragment and
> direct come back to 1st fragment.
> 但是当用户访问 3rd fragment 并直接返回 1st fragment 时,我必须隐藏 2nd fragment

here is my code:
以下是我的代码:

open root fragment[1] from activity with fragmentTransaction.add
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](with addToBackStack) >[2] :opening 2nd fragment
[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](without addToBackStack) > [3]
[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();
    }
}

Now open 3rd fragment and click showing 3rd fragment+1st fragment.
现在打开 3rd fragment,并单击显示第三个 fragment + 第一个 fragment。

英文:

I open fragments like this: [1]+with addToBackStack) > [2](without addToBackStack) > [3]

in Fragment 3 when click onBackPress Button then 2nd is not visible but 3rd override 1.
3rd Fragment 在不使用 addToBackStack 的情况下,会覆盖 1st Fragment 的返回操作。

If I use addToBackStack in 2nd fragment also, then everything is fine(No override issue).

> But I have to hide 2nd fragment when user visit 3rd fragment and
> direct come back to 1st fragment.

here is my code:

open root fragment[1] from activity with fragmentTransaction.add

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](with addToBackStack) >[2] :opening 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](without 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();
    }
}

Now open 3rd fragment and click showing 3rd fragment+1st fragment.

答案1

得分: 1

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()
        }
    }
}
英文:

Try this

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()
        }
    }
}

答案2

得分: 0

用户在进行操作时,按顺序转至 [1] > [2] > [3]

然后在 onBackPress 时,用户会返回到 [3] > [1]

>最后,我找到了解决方案。
>>在 [第二] 片段 内遵循这两个步骤:
>
> 步骤1. 在调用 [3] fragment 之前,只需添加 fragmentManager.popBackStack();(此行后,
> 它会销毁/弹出第二片段并重新打开第一片段,然后再打开第三片段。也就是说,当用户在第三片段上按返回按钮时,
> 在最近的片段中,有第一片段而不是第二片段)

> 步骤2. 也在 fragmentTransaction 中加入 .addToBackStack(null)

[第二] 片段:在此处,单击按钮后我们将打开 第三片段

if (getActivity()!=null){
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

    //此 popBackStack 将关闭这个 [2] 片段。
    fragmentManager.popBackStack(); //<<<<<<<在 "fragmentTransaction" 之前添加此行。

    //现在在这里打开 [3] 片段,现在最近的片段只有 [1] 片段,没有 [2] 片段。
    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(), "出现了一些问题 (#202)", Toast.LENGTH_SHORT).show();
}

(^_^) 祝您编码愉快

英文:

When user go [1] > [2] > [3]

Then onBackPress user will be [3] > [1].

>Finally, I got Solution.
>> follow these 2 steps inside [2nd] fragment:
>
> Step1. Just add fragmentManager.popBackStack(); before fragmentTransaction mean calling [3] fragment. (after this line,
> It destroy/pop 2nd fragment and open 1st fragment again before open
> 3rd fragment. It mean when users press back button on 3rd fragment.
> then in recent fragment, there is 1st fragment not 2nd)

> Step2. add .addToBackStack(null) also with fragmentTransaction

3rd Fragment 在不使用 addToBackStack 的情况下,会覆盖 1st Fragment 的返回操作。

[2] Fragment : here on button click we will open 3rd fragment .

if (getActivity()!=null){
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();

    //this popBackStack will close this [2]fragment.
    fragmentManager.popBackStack(); //<<<<<<<just add this before "fragmentTransaction".

    //now open [3]fragment here, Now in recent fragment is only [1]fragment not [2].
    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); //<<<<<<<<<add this also
    fragmentTransaction.commit();

} else {
    Toast.makeText(getContext(), "Something went wrong (#202)", Toast.LENGTH_SHORT).show();
}

(^_^) Happy Coding

huangapple
  • 本文由 发表于 2023年4月17日 15:06:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76032479.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定