Android livedata losing active observers when changing fragment

475 Views Asked by At

I have an application which follows a single activity pattern with several fragments which uses Jetpack navigation to navigate within a drawer.

On one fragment, i have a live data accounts which i observe from my fragment as such:

accountFragment.kt

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    //Observe accounts list being updated
    observeAccountsEvent()
}

private fun observeAccountsEvent() {
        with(viewModel) {
            accounts.observe(viewLifecycleOwner, Observer { accounts ->
                if (accounts != null) {
                    //set loading flag to hide progress bar
                    doneLoading()

                //Alternate visibility for account list and empty view
                alternateAccountListVisibility(accounts.size)

                //Submit the cards
                initiateAccountList().submitList(accounts)
            }
        })
    }
}

accountViewModel.kt

private val sortedByName = Transformations.map(_currentAccountsExposed) {
    it.sortedBy { account ->
        account.accountName.toLowerCase(
            Locale.ROOT
        )
    }
}

private val sortedByUsername = Transformations.map(_currentAccountsExposed) {
    it.sortedBy { account ->
        account.username.toLowerCase(
            Locale.ROOT
        )
    }
}
...

init {

    //Load the accounts for the first time
    loadAccounts()

}

val accounts = Transformations.switchMap(_sort) {
    Log.i("ACCOUNT_SORT", "7. Started Transformation...")
    when (true) {
        it.name -> {
            //Log.i("ACCOUNT_SORT", "4. Name sorting...")
            sortedByName
        }
        it.username -> {
            //Log.i("ACCOUNT_SORT", "4. Username sorting...")
            sortedByUsername
        }
        it.email -> {
            //Log.i("ACCOUNT_SORT", "4. Email sorting...")
            sortedByEmail
        }
        it.website -> {
            //Log.i("ACCOUNT_SORT", "4. Website sorting...")
            sortedByWebsite
        }
        it.authType -> {
            //Log.i("ACCOUNT_SORT", "4. Auth Type sorting...")
            sortedByAuthType
        }
        else ->  {
            //Log.i("ACCOUNT_SORT", "2. current sorting...")
            _currentAccountsExposed
        }
    }
}

private fun loadAccounts() {
    _currentAccountsExposed.addSource(AccountDao().getAll(getUserID())) { snapshot ->
        _currentAccountsExposed.value = decomposeDataSnapshots(snapshot)
    }
}

internal fun click() {
  Log.i("ACCOUNT_SORT", "Accounts: ${accounts.hasActiveObservers()}")
}

I have a button which when i click, triggers the click function in the view model. The function click logs whether the accounts live data has active observers or not.

When my app launches, it comes directly to the accounts fragment. When i click the button, it displays true. But if i change fragment in the drawer and come back to accountFragment and click the button, it shows false. Can you please help on why my account live data is losing its observers?

I have another cardsFragment which fetches data the same way as the accounts fragment and it doesn't lose its active observers. My source is from firebase and and i have no problem fetching data from it. It also works fine for the cardsFragment and the code is identical to accounts fragment. I am having problem just with the accounts fragment. Can someone please help me?

0

There are 0 best solutions below