Let's setup the view-binding for the ResultActivity by adding a private member variable binding and also updating the onCreate method appropriately. Also, let's add stubs for methods updateUI(), getData(), and setListeners(), so that the class looks like this:
Next, let's fill in the code for the method updateUI(). The work of the method is to set one of the two UI modes: the 'busy' mode or the other one showing the QR code:
The method setListeners() will set the size of the QR code image to be displayed and then show the appropriate image, depending on which of the buttons is pressed. There is a call to a getImage() method, which we have not yet implemented, just add an empty stub method for now. Before we get to the code for setListeners(), must add an enum to handle the 3 image sizes. Create the following enum in a new file:
Then back in ResultActivity class, add a private member variable to represent the ImageSize with a default value of small:
Now the code for setListeners() will set the desired image size... Don't forget the stub for getImage():
The getImage() method will be implemented in a different section of this tutorial, but for now, empty method stub will be enough.
Moving on, there are situations where we will need to display a message to the user and then navigate back to the previous activity (the one for user input). For that, we will create 2 methods called goBackAfterMessage(). While having the same name, it will be the parameters that will be different:
And because we are using the OnBackPressedDispatcher , API levels 33 and up need the following application tag attribute in your your AndroidManifest.xml file:
And the last thing for this section is to add the implementation for the getData() method. All it does it to get the user input added to the intent's extras. If the message to encode in the QR code is missing, an appropriate message is displayed and the app navigates back to the previous screen. Add the following values to your strings.xml file:
The data read from the intent's extras will be set to private member variables of the ResultActivity class:
Finally, the getData() method:
In the next section, we will implement the code to generate a QR code as an image.