FragmentTransaction : replace and addToBackStack not working together?

后端 未结 2 1533
暖寄归人
暖寄归人 2020-12-25 08:35

I\'m fairly new to Android development and now running in to weird behaviour.

  • I have an empty FrameLayout to be the container of a fragment.
  • If user p
2条回答
  •  自闭症患者
    2020-12-25 09:14

    addToBackstack creates a snapshot of your fragments state. Which means when you press the back button, you are actually reverting to the last state that addToBackstack was called on.

    In your case, you add a Fragment. The back button would remove this added fragment. When you call replace, and add to backstack again, you now have two states on the backstack (1. when you had the first fragment added, 2. when you had no fragments added). If you the back button to remove the current fragment, the don't use addToBackstack. Only use addToBackstack when you want to preserve the state of fragments in a view.

提交回复
热议问题