I'm creating an app that has BottomNavigation
with Fragment
. I'm following the sample here https://github.com/android/architecture-components-samples/tree/master/NavigationAdvancedSample, but I see
onCreateView
and all the following lifecycle callbacks are called when switching between tabs. I want to make the network call just once because I don't want to keep updating the UI or even waste resources to communicate with server. I can only think of using flag or check if cache is null, but somehow I feel there's gonna be a cleaner way.
You should make network calls in activity in that case, and then once you need the related data you can request the same from activity. You may refer below link to understand how you can fetch/send data between activity and fragment.
Communicating between a fragment and an activity - best practices
Also, Fragment or Activity are not right places to put network calls, would suggest you to follow a architectural pattern like MVVM or MVP and make use of models, repositories, etc. for network calls.