In our previous video tutorial (Configuring a Dev Environment for Backendless Android App) we have imported all the data, created data tables and registered a user. We have used the order_success module which is the final module we will build in this tutorial series.
Today we will be talking about the user registration API and focus on the registration module.
To master the topic you can review the Backendless video tutorial below or the text tutorial beneath the video.
User Registration Screen and API
Open Android Studio and select the registration module. You will see the following structure.
First of all, the registration module must be pointed to your Backendless backend that has been configured in the previous article. For this you have to copy the two lines selected in the screenshot below from BackendSettings in order_success and paste them into BackendSettings class in the registration module.
Lets take a look at the registration API and registration module in general.
There will appear a layout of the registration form. The layout is very simple and holistic. It contains all the labels and text inputs for the registration form. The registration form contains fields for name, email address, password and the REGISTER button.
One thing you should keep in mind is that the concept of a user in this application is represented by 3 properties – a name, an email address and a password. If you return to Backendless Console and click USERS icon on the left, you will see that users properties contain these same 3 properties.
This is not a coincidence because the registration of the user and the user entity description should match between
- what you have on the backend as shown in the Backendless Console
- what you have on the client side which is the layout that we have in the application.
Every single field on the screen has an ID. Name field’s ID is named nameField. The same with emailField and passwordField accordingly. The REGISTER button’s ID is registerButton. These IDs are important because they will be used in the code of the application.
You can see the way it is all programmed in RegistrationActivity class. The onCreate() method is called as soon as the registration activity is started.
There is Backendless.initApp call. It is very important because every single application that uses Backendless must call this method as it specifies the actual application ID and your secret key that we have imported in the previous tutorial.
Calling this method is going be extremely vital for your application, so it should be the very first thing that you do in your code.
How does the Backendless registration process work?
It’s very simple. A user fills out a form and clicks the REGISTER button. Then we make a request to the server to register the user. In order to link that button with a piece of code which runs whenever you click it, we create a button click listener.
To have a deeper look at the method createRegisterButtonClickListener hold Ctrl and click on its name stepping in there.
Here we basically put together the logic that extracts all the data from the individual fields:
- we get the Field references
- we obtain the values from those fields.
Also we have a validation call with the values to be validated. We create an registration callback that will be invoked whenever Backendless AndroidSDK receives a response from backend. Finally, we are going to invoke a method to register the user.
All of this will be happening whenever we click the Register button in the UI.
Now lets review the registerUser method. Hold Ctrl and click on its name in order to step in there.
The actual interaction with Backendless API is located here. As you can see we create an instance of a special class called BackendlessUser. This class comes from the Backendless library. We populate it with various properties: email, password, name – exactly what we get from the user in the registration form. Finally, we make an API call where we pass a user object in. So literally, it takes just ONE line of code to register a user with the backend. As a result of this line, your user entities will start showing up on the backend.
The registration callback is an instance of an AsyncCallback class. This class contains two methods:
- success callback
- error (failure) callback.
We’d created this callback in the click listener before and we’ll review it next.
But for now let’s see how it works.
Select registration from the configuration settings and click Run
Now this form is up. In the module that we’ve started there is only one form – one piece of logic for the registration screen.
We create a user here just to show you how it works in this isolated module.
A sample user will be called Bob the Builder. Fill the name / address / password fields and click the REGISTER button.
Clicking the REGISTER button will send a registration request. At the end we have the user registered. When the successful response is received in callback, the registered user’s objectId is displayed.
If we switch to the Backendless Console we’ll see that Bob the Builder is the registered user of the application.
To finish the overview of Backendless user registration we‘ll return to Android Studio.
At the beginning of this tutorial we talked about the registration callback. Now we’ll see how the callbacks work in Backendless because we will be running into callbacks with every single API call that is done on the Android side for the reason that on Android you have to use the asynchronous API.
Go to createRegistrationCallback method to get the code responsible for creating a callback instance.
Here we create LoadingCallback instance which is a custom class that we created specifically for this sample application. The approach that is used here is actually recommended because it provides a lot of versatility. Here in LoadingCallback there is an overridden handleResponse method. This method will be invoked every time when a user is registered within that application. The parameter that comes back is an instance of BackendlessUser. This user actually represents a registered user on the backend. He already has an objectId property and all the proper elements of the created objects for the backend prospective.
LoadingCallback implements AsyncCallback interface which is included into the Backendless SDK for Android/Java.
There are 2 core methods that have to be implemented:
- handleResponse: in this method we have a default implementation that doesn’t do anything besides cancelling ProgressDialog.
- handleFault: this method is invoked anytime a server returns an error. This could be application level errors about registering an already existing user, as validation happens or with any other reason.
As you see we’ve just created a method that overrides handleResponse. This approach is very convenient because it also includes the ProgressDialog which could be shown or dismissed. Besides, the logic for showing and hiding this ProgressDialog is quite convenient. Take a look at these methods and see if you can start using them in your project as well.
In the next tutorial we‘ll start reviewing the process of logging in and entering the application.
Enjoy and Happy coding!