Reloading data after every rotation I fetch data in onCreate and observe in onCreateView(). I want to know after rotating the phone(or after configuration changes data is reloaded again as a result I have these logs before rotation
fetchConfig ->observe
and after rotating I have
observe ->fetchConfig ->observe
How I can prevent reloading data second time? I have added in fetchConfig()
if(customerConfigData.value==null) {}
but I am not sure is it the best solution
private val viewModel: HomeViewModel by lazyViewModel()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.fetchConfig()
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
viewModel.customerConfigData.observe(viewLifecycleOwner, Observer {
Log.i("test","observe")
})
return inflater.inflate(R.layout.fragment_home,container,false)
}
fun fetchConfig() {
Log.i("test","fetchConfig")
uiScope.launch {
val configEndpoint = EnigmaRiverContext.getExposureBaseUrl().append("v1/customer").append(AppConstants.CUSTOMER_UNIT)
.append("businessunit").append(AppConstants.BUSINESS_UNIT).append("defaultConfig").append("?preview=true")
val parsedData = homeRepository.fetchConfig(configEndpoint, GetConfigCall())
customerConfigMutableData.postValue(parsedData)
}
}