Join Albert Lo for an in-depth discussion in this video Select a Google account, part of Android Development: Modern Android UI Design Elements.
- [Commentator] Now we are ready to add the code for the workflow of adding or selecting a Google account. To do that, let's scroll up to on view created. And just below on view created method we're going to create a new method, it's going to be a private method and we're going to call it check account or load albums. In this method, we're going to do a couple of things. First we're going to check if the picasa client is initialized.
If our picasa client is initialized we simply call load albums. If it is not initialized, the next thing we want to do is to check whether we have the Google account saved in shared prefs, so I'm going to create a string for our Google account. And make a call to preference manager to get our default shared preferences. And we will pass in our context.
And we want to retrieve our Google account name as a string. So we will call the get string method. And we will pass in a key pref account. With a default value of null. Let's go ahead and define pref account. This is the string key for our shared preferences. So I'm going to go Command+C to copy pref account, and I'm going to go to the beginning of the file.
Let's first create a private tag. It'll be a static final string. And the tag will be the Google photos fragment name. Now we can define our pref account, it's also going to be a static final string. And we will concatenate tag with the string value pref account.
Okay, let's go back down to check account or load albums. Now we have the code for retrieving the preferences for our saved account. Next we are going to check whether our saved account string exists or not. So we will call text utils is empty. Passing in our saved account string, and I noticed that our saved account check is incorrect so I'm going to make the corrections here, so now it reads if we have an actual saved account string, then we can make our picasa call for the workflow for selecting a Google account.
Pass in our saved account. And we pass in a type that was defined in picasa client. Now we make our call to set our account, which is really to start the workflow for asking the user to select a Google account. Again this is an RX Java call so we're going to chain some calls here, first one is observe on, and we're going to observe on the main thread.
And the next one is we're going to add a subscriber. The subscriber that we add will be a completable subscriber. So once we have our Google account, we are going to load albums. Now to handle the results of this picasa client call, we need to go to a main activity and define an on activity results, so let's switch over to main activity.
Now we're going to define our on activity result. It's an activity life cycle method call. And it will have three parameters. The first parameter is request code. The next parameter is the result code. And the final parameter is the intent.
So the workflow of selecting a Google account, if it was successful then we want to pass that result back to our fragment, and there's a request code that we can look for that's defined by picasa client so let's check for the request code here. And we're going to create a private method called handle activity result and pass in all of our parameters to that method.
We're going to pass in request code, result code, and our intent. We are also going to pass in an extra parameter, because we're going to ask this method to look for our Google photos fragment class. Let's define our handle result here. And I'm just going to copy these parameters.
With the extra Google photos fragment class parameter. So what we're going to do here is we're going to iterate through our list of available fragments. And match it up with the one that was passed in to this method, so let's get our list of available fragments. And we will call our get support fragment manager to do that.
And we will make the call to get fragments. Now we're going to iterate through our list of available fragments. And we're going to look for a match between what was passed in and what we have in our list. So if our class object is an instance of the current iteration of fragment, so if we have a match, we are going to pass our on activity result over to our fragment to handle.
Okay, so there's also a build error with request account picker here, so let's go in and fix this error by pressing Control+B to go into picasa client. The error that needs to be fixed is to change the scope of request account picker from private to public. Now let's close this file and go back to our main activity and now we are done, there's no more errors and we're done with handle activity results.
- Creating a custom layout with XML
- Setting up OnClick handlers
- Making API calls
- Creating custom views
- Implementing ViewHolder objects
- Signing certificates
- Adding a checkbox