Navigation Component prevent to recreate fragment on back press

陌路散爱 提交于 2019-12-07 03:58:48

问题


I'm using the Jetpack Navigation Component in my project with a signle activity and some fragments.

I have a fragment with a list that fills from server side. I call getDataFromServer on onViewCreated method, then when a user clicked on an item new fragment shows.

The problem is when I press the back button, onViewCreated calls again in my list fragment.

so how can I prevent my first fragment to recreating again? I don't want to onViewCreated calls.


回答1:


You can't prevent calling onViewCreated method or any method of your fragment when back button pressed so you should better use view model with your list fragment and get data from server in your view model. Avoid getting data from server in your fragment since you already using Navigation UI.



来源:https://stackoverflow.com/questions/54553333/navigation-component-prevent-to-recreate-fragment-on-back-press

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