How to avoid fragment recreation when tap back button using navigation architecture actions?

女生的网名这么多〃 提交于 2020-02-03 10:45:27

问题


I am using android studio navigation component to create some fragments and switch between them using the actions. Problem is that each time I tap Back button to go back from a Fragment2 to Fragment1, Fragment one is recreated.

I want to avoid that.

I found solutions when one don't use navigation component and simply can replace ".replace" with ".add", but can't find any information on how to do the same with navigation component.


回答1:


I think it's a normal thing that your fragment 1 is recreated, I think you have a fragment container. And when you move from fragment 1 to fragment 2, you replace the fragment 1 with fragment 2 layout using FragmentTransaction.commit() If you want to control the back button of your App, you can override the Onbackpresed method in your Mainactivity class. what specifically you want to do when you press the back button ??



来源:https://stackoverflow.com/questions/54266152/how-to-avoid-fragment-recreation-when-tap-back-button-using-navigation-architect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!