Search code examples
androidmvvmrx-java2android-livedataandroid-viewmodel

ViewModel: Check if fragment is resume or stop


I perform network stuff inside ViewModel that repeat its fetching with interval of 10 seconds with RxJava. However I do not want to fetch data from this API when my Fragment is not visible to screen for example is when I opened an Activity on a top of it I will have a code like if(!isResume) return. My question will be, is it okay to use a MutableLiveData<Boolean> where value will be updated in onResume and onPause so I can just ignore network request if not resume or is it against the concept of MVVM? We can do this but not sure if it is okay to do so.


Solution

  • You can make your ViewModel implement the interface DefaultLifecycleObserver (https://developer.android.com/reference/androidx/lifecycle/DefaultLifecycleObserver), and then you override the methods onResume and onPause. Update an internal boolean variable, e.g. isResumed, when those methods are called and you'll be able to check in other methods if the corresponding fragment is on the resumed state.

    class YourViewModel : DefaultLifecycleObserver {
        private var isResumed = false
    
        override fun onResume(owner: LifecycleOwner) {
            isResumed = true
        }
    
        override fun onPause(owner: LifecycleOwner) {
            isResumed = false
        }
    }
    

    You also need to update your Fragment code to add that ViewModel as a lifecycle observer:

    class YourFragment : Fragment() {
        private val viewModel: YourViewModel // initialize it
    
        override fun onViewCreated(...) {
            lifecycle.addObserver(viewModel)
        }
    }