2

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)
}

}
I.S
  • 1,904
  • 2
  • 25
  • 48

3 Answers3

3

One solution I think would be to move call to fetchConfig() in to the init block of your ViewModel

John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
1

As you can see, your method has a parameter called savedInstanceState: Bundle?. This bundle is able to save the state of the app when the configuration changes. So, you can put here any flag you want. Now, remember that ViewModels are designed to be implemented with a good code base. So, you need to separate the Ui layer from the business layer. The fetch configuration method should be in another class which doesn't depend on the Android lifecycle. I strongly recommend reading these articles.

https://medium.com/androiddevelopers/viewmodels-persistence-onsaveinstancestate-restoring-ui-state-and-loaders-fc7cc4a6c090

https://developer.android.com/jetpack/docs/guide

In conclusion. Your solution is not the best. The best approach is to implement a correct layer for fetching the info in a way that it doesn't depend on the Android lifecycle.

Community
  • 1
  • 1
  • You mean keep state if it data is fetched or not in savedInstanceState? – I.S Feb 15 '19 at 16:40
  • That can be an approach. But again, the data must be in somewhere else. That place should make the validation about what retrieve to the view model. – Jhon Fredy Trujillo Ortega Feb 15 '19 at 18:24
  • I don't get what you mean by "That place should make the validation about what retrieve to the view model." Can you explain, please ? – I.S Feb 15 '19 at 23:42
  • Fredy Trujiillo I have read the arcticles you posted,Thanks if I combine with savedInstanceState But in a case `The activity is created after being stopped by the system:` in this case savedInstanceState would not be null but data in viewModel is null how should I know that data is null ? – I.S Feb 19 '19 at 10:49
0

I too had similar issue. I was suggested to try Event wrapper for LiveData, it had solved my problem:)

Here's the link: How to stop LiveData event being triggered more than Once

Hope this helps!

Ningan
  • 151
  • 1
  • 8
  • 1
    I don't have enough reputation to add a comment. Only way to convey message was to add answer. – Ningan Feb 15 '19 at 16:39