Hello Devs, can anyone help me with this. So I’ve ...
# android
n
Hello Devs, can anyone help me with this. So I’ve a very famous problem with Navigation Component of fragment recreating when back is pressed, it happens in this case : There are 2 fragments A and B where A is having list of items: 1. Scroll down to bottom of list on A 2. Navigate from A to B 3. Press back button 4. Fragment A state is changed and it’s not at the bottom of the list How can I prevent Fragment A state from getting destroyed and recreated ?
😶 2
d
@Naveen Here fragment is not destroying, only the view is destroying. When fragment loading from backstack new view instance is used, here you can maintain scroll position in viewmodel level and when fragment appears time, scroll to the particular pos.
n
Got it. thanks @Deepak k.k Also if I want to fetch data from server only once then which method should i use in fragment A ? As onCreateView and onViewCreated are call each time when we come back to fragment A
d
You can call it from view model init or fragment onAttach
n
Thanks a lot man. I'll try this
Bro can you share any example of this if you've seen it in any GitHub repo?
😂 1