Intro

What to do when you need to start an activity just to get some data from this activity and then go back to your previous activity. When you want to send a message and you go to your list of contacts just to select a contact and then go back to continue writing your message. To achieve this we must use the startActivityForResult() method.To demonstrate how this method can be used we will create an app that will simulate a messaging service.

Exercise

In our app we will have two activities. In the first activity, that we will call main activity, there will be a message writing layout, with Send To option from where we will start our second activity to select a contact, an edit text view where we will write and a send button, that will be there just for show off without any real function for our exercise. In our second activity we will have a list of contacts to chose from. We will be using Butterknife and RecyclerView in this exercise, but won’t get into much details about them, because we have separate posts explaining these elements. In this app, when the user clicks on the Send To field, he will be taken in our second activity where we have our contacts. When the user clicks on one of the contacts, the app will go back in the first activity and in the field next to the Send To field we will have the contacts email address(the result taken from the second activity).

First we will create the layout. For our first activity, to simulate a messaging service:

For our second activity we will be using a recyclerView to show our list of contacts:

If you are using androidx you will need to add this dependency to your build.gradle file:

In the layout we will need to create the layout for a single item in the recyclerView, so we are creating a new layout file item_contact.xml:

With that we are done with the simple layout for this exercise.

First we will create the list of contacts in our second activity. For this we will create a simple model for Contact, with just two properties contact name and email address. The model class will look like this:

Now that we have created the model, we set the adapter for the recyclerView. We create a class ContactAdapter (as we said we won’t get into much detail about the recyclerView, you can find posts about it on our page that explain it in detail). Inside the adapter we will create an interface that will be used to override the on click method in our Contacts activity:

The adapter class will look like this:

The scenario for this app is, we want to write a message and when we click on Sent To, we want to go to our second activity to select a receiver from our list of contacts. When we click on one of the contacts, we want to go back in our first activity and we want to display the  email of our selected contact the the field next to Sent To. In our Contacts activity we will create a dummy data contacts list and we will set the adapter.

To start our second activity for result, in our main activity first we declare an int variable, because we need to pass an additional integer argument to the startActivityForResult() method, so we declare and initialize:

The value is set to 1 because if everything goes like it should, then the value of RESULT_OK will be 1 and in our onActivityResult() method  we can check if (requestCode == RESULT_OK).

After this in our main activity we create the on click method for the Send To field, where we will have the startActivityForResult() method:

Now when we click on the Send To field we will start Contact activity. Next step is to set our result in the Contact activity. First we implement the onItemClickListener interface we created in the adapter, and we override the on-click method:

Now when we click on an item from the contacts list it will set the result to be the contact’s email address. Next thing we must do is to retrieve the result in our main activity. To do this, we will use the onActivityResult() method in main activity:

This is how our app looks like:

app_preview

As always there are many ways to do a single task, so if you have a better solution please share it with us 🙂

If you are interested you can find the whole project here https://github.com/AleksandarGulevski/ActivityForResultAppExercise

Happy coding!

 

 

 

 

Spread the love

Leave a Reply

Your email address will not be published. Required fields are marked *