I have 2 fragments in view pager.They both start to make network requests at the same time.I want to speed up first fragment by delaying second until the first loads the data.How can i do that? Im coding in java.
Edit: When both fragments loads the data it takes additional 2 seconds to finish loading first fragment compared to when i make network request only with first fragment(when testing) because view pager loads both tabs at the same time.
Edit2: found same question at: link One answer suggests creating interface for the first fragment.
interface TabView {
fun prepareTab()
fun onTabOpened()
fun onTabClosed()
}
Problem is i cant use interfaces because my first tab uses root fragment from example that ive found here: link
Edit3: my network request in second fragment: Implementation of network bound resource
An alternative to interfaces is a shared viewmodel for communicating between fragments (there are other methods as well) see androidwave.com/fragment-communication-using-viewmodel for example.
You would then you set a flag in the shared viewmodel in onChanged
of the LiveData observer of Fragment1 and then in Fragment2 observes this flag in the shared viewmodel and does not registerLiveDataObservers();
until is in the the right state to say Fragment1 has loaded it's data (data has size > 0 )