Search code examples
androidfirebasemvvmfirebase-authenticationandroid-architecture-navigation

The proper way to signOut using Firebase in MVVM android


I am using a firebase and firestore in the project. I am also using a single activity architecture with a navigation component. When I am doing logout, there are still some active listeners in my SharedViewModel. My fragment is calling logout methods like this:

FirebaseAuth.getInstance().signOut()
navController.navigateToLoginActivity()

What this does is firstly signing out from firebase and then starting my login activity and destroying my current activity. All the firebase listeners are disposed/canceled in viewModel.onCleared(), but this happens after the sign out, so I got some errors. Is it how this should work or am I missing something?


Solution

  • Any listeners that you have open on queries that require authentication are going to be immediately cut off if you sign out the user. That's totally expected. If you don't like seeing the errors, I suggest writing code to remove all those listeners first before signing out. While it's not required, it makes it clear in your code what is expected to happen.