Building Apps With Android Studio: Accessing Activity Results


Welcome to the final part of this review of the Pluralsight course Android for .NET Developers: 2 Building Apps with Android Studio by Jim Wilson

Jim has over 30 years of software engineering experience, with the past 15 years heavily focused on creating mobile device and location-based solutions. He has co-founded multiple software-related startups and has served in a consulting role at several more.

After nearly a decade as a Microsoft Device Application Development MVP, Jim now focuses on developing Android and iOS device applications. Jim’s passion is mentoring software developers. He blogs at

This is the second course in a series of four courses which Jim has created for Pluralsight. I have also reviewed his first course which is Android For .NET Developers 1: Getting Started. If you are completely new to Android you should begin there. But if you have some previous experience with Android Development you can start here.

Accessing Activity Results

Working with Activity Results

Jim begins by showing us what we will be building in this module: an activity with “provide information” and “take picture” buttons.

The “provide information” button displays a screen where we select one of Jim’s Pluralsight courses:

  • Android for .NET Devs: Getting Started
  • Android Programming with Intents
  • Improving UI Interaction with Android ActionBar
  • Android Async Programming and Services
  • Android 4.0 New Features

There are also text boxes to enter name and email address.

When we click “done” all of the details are displayed back on the original screen.

There’s also functionality to take pictures and display them on screen as well.

Handling Activity Results

So how does this work?

We use a method called startActivityForResult. As the other activity finishes, we get information back from that activity.

There’s a method called setResult, and this can set a code indicating success, failure or a custom code. It can also set an Intent.

When the activity calls finish, control is passed back to the Android layer, and then Android calls into out onActivityResult method.

In this course we’re using it for dealing with the camera. In the future courses in the Android for .NET Developers series, we’ll see more powerful mechanisms for taking advantage and leveraging the capabilities of the platform.

Starting Custom Activity for Results

There are three Text Views on the main activity.

The “provide” activity has radio buttons and EditText views.

Jim runs through the setupViews code, and then implements the handleMoreInformationButton method.

We use an intent, and startActivityForResult, which takes a request code. We need to specify which request we need, and an request code is just an integer that we define.

Custom Activity Return Results

First Jim explains the hint attribute that the EditText view has.

We see in the XML file android:hint is set to the name of the text box. This is displayed inside the textbox, meaning we don’t need to take up extra space with labels outside the text box.

We also see each EditText view has a different android:inputType value and the “textEmailAddress” value has the @ symbol on the main keyboard which is much more user friendly. In fact there are many text fields predefined for us such as:

  • Plain Text
  • Person Name
  • Password
  • Password (numeric)
  • E-mail
  • Phone
  • Postal Address
  • Multiline Text
  • Time
  • Date
  • Number
  • Number (Signed)
  • Number (Decimal)

Jim demonstrates writing the handleDoneButton method. This calls our existing methods getSelectedValueOfRadioGroup and getEditTextValue.

We learn a handy productivity tip. Press Ctrl+Shift+Enter and it auto-completes the statement for us. The usual use cases are:

  • Add a semicolon at the end of the line, even if you are not at the end of the line
  • Add the parentheses and curly braces after an if, while or for
  • Add the curly braces after a method declaration

Again we use setResult and pass in a request code and an intent, but this time we put extras into our intent with the putExtra method.

Jim says this loads up the data to be returned, but the data won’t be returned until we call the finish() method.

Handling Returned Activity Results

Here we override onActivityResult, removing the call to the base class and adding a switch statement.

The bulk of the work is done in a new method called handleProvideInfoResult. Jim warns that if the user pressing the back button, the result code is RESULT_CANCEL, so we must check that we have the correct result code in here.

We get the data from the other activity and assign the values as local strings variables. We then use each of these as the arguments for our text boxes when we invoke setText on them.

And if the user presses the back button, we just display a toast message.

Walkthrough: Handling Activity Results

This is a demonstration of the various activities working together. We hit a breakpoint and step through, and everything works correctly.

Starting Camera from Your App

Android has a class called MediaStore. This lesson is just a quick intoduction, but Jim has another Pluralsight course called Android Photo and Video Programming which goes into much more depth. I will also be reviewing that course for you soon.

We see which has a method generateTimeStampPhotoFileUri, and this takes the current date and time and uses that as part of the filename of a JPEG file. There’s also a method called getPhotoDirectory.

Handling Camera Results

A few updates are needed in, starting with a extra case statement for the take picture request code, which calls a new method handleTakePictureResult.

Similarly to the earlier lesson, we display a toast message if the user presses the back button. But if the result code is OK, we get the photo path name and supply it to our photoHelper to add the photo to the media store and display the thumbnail.

Walkthrough: Starting Camera and Handling Results

We run our program in the emulate, step through the code and see everything works well.

Configuring the Emulator for Camera Support

We go into the Android Virtual Device manager, select our AVD and edit.

There are two dropdown lists: front camera, and back camera. The options are:

  • None
  • Emulated (greyscale image)
  • Webcam() (use a camera that is connected to your PC)

Jim also recommends at least 512MB size for the SD card.

The End

This is the last course in the series that I will be reviewing, at least for the next few months, because I am following the Android learning path this month, and the last two courses in Jim’s series aren’t included in it.

However if you’ve enjoyed the series so far, don’t forget these two follow up courses that he has produced:

Android for .NET Developers: 3 Adopting The Android Mindset

Android for .NET Developers: 4 Understanding The Android Platform

The next course in the Android Learning Path is Exploring Android Studio.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s