Replacing a Fragment with itself does not show anything

后端 未结 4 861
猫巷女王i
猫巷女王i 2021-01-12 20:19

I\'m trying to decide and show a fragment in activity\'s onResume method, but in case a previously added fragment is chosen again, then the activity goes bl

4条回答
  •  渐次进展
    2021-01-12 21:08

    When referencing back to a created Fragment please do make sure to try adding the

    fragmentTransaction.addToBackStack(null); 
    

    method right before committing so that your Fragment is resumed instead of destroyed as mentioned in the developer guides.

    If you don't call addToBackStack() when you perform a transaction that removes a fragment, then that fragment is destroyed when the transaction is committed and the user cannot navigate back to it. Whereas, if you do call addToBackStack() when removing a fragment, then the fragment is stopped and is later resumed if the user navigates back.

    You can find this at the end of this page.

提交回复
热议问题