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.
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