If you haven’t done the previous chapters, then from GitHub you can download the starter project for this chapter.
Step 1 – Add button to activity_main.xml
When we register, login or we are logged in at the start up of the app, we are going to be redirected to the MainActivity::class. It means, that we will implement there a log out button. For this we have to add a Button to the .xml file of the MainActivity::class. So open from the “res”–“layout” folders the activity_main.xml file and add after the TextView this Button.
Log out Button
Step 2 – logOutUser()
In the next step we are going to add the logOutUser() method to the UserRepository::class. This gonna be again a suspended function, and does not return any value.
suspend fun logOutUser()
Step 3 – The implementation
As we did before, the implementation will be in the UserRepositoryImpl::class. This method will be very simple. We are going to call on the FirebaseAuth the signOut() method. So add it to the UserRepositoryImpl::class.
Step 4 – The logOutUser() in the ViewModel
The last function what we have to implement goes to the FirebaseViewModel::class. In this class we gonna call the suspended function of logOutUser() from the implementation class.
Because of the suspended keyword, we can call this function only inside of a CoroutineScope. In a class, which extends ViewModel, we can use viewModelScope. This scope is valid only inside of the FirebaseViewModel::class.
On this “viewModelScope” we are going to call the launch lamba, then inside of the curly braces we can call the logOutUser() from the implementation class.
logOutUser() in the ViewModel
Step 5 – The click listener
Finally we are going to add the logOut() method to the MainActivity::class.
So open the activity and add this function there.
As you can see, first we are going to call the logOutUser() method on the FirebaeViewModel::class, then we will start the LoginActivtiy::class.
The startLoginAcitvity() function looks like this:
In this method we will create a new explicit Intent, which is needed to start a component in our app.
The second line will create a new flag for the Intent.
- FLAG_ACTIVITY_CLEAR_TASK: this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started.
- FLAG_ACTIVITY_NEW_TASK: this activity will become the start of a new task on this history stack.
Finally we can start the LoginActivity::class using the startActivity(intent) method.
The click listener
At the beginning of this chapter we have added a new button to the activity_main.xml. Here we are going to call on it a click listener lambda. Inside of it we are going to call the logOut() method.
The code looks like this:
Run the app
So now we can test the new logging out feature. For this start the app on your emulator or on you physical device.
The source code available on GitHub
I hope the description was understandable and clear. But if you have still questions, then leave me comments below! 😉
Have a nice a day! 🙂