Group Services: Technology Consulting
phone +91-9999-283-283/9540-283-283
email info@sisoft.in

Using Explicit Intent to send Data from one screen to another

In this example, we will be see how to use an Explicit Intent to send data from one screen to annother(Inter Activity Data Transfer)

Steps to develop this can be summarized below -

Used Concepts & Used Version:

Now, let us begin the development...

1.BatmanBegins.class (First Activity)

This is going to be our launcher activity for the app. We will have a textview and a button in this activity. The button takes the user to the next screen which is TheDarkKnight.class activity. We use explicit intent to make this happen. In the given code below, we make a object - i1 - of the Intent class. Two parameters are provided to the Intent constructor - "msg": a key and "I liked Batman Begins": a String(data). We use 'putextra method for this.' Finally the intent is fired by calling startActivity(i1) method.

2. Layout for BatmanBegins.class

Below is the layout for the same activity - BatmanBegins.class We have used a relative layout to hold a textview and a button. For the button , we havee used 'onClick' attribute. So whenever the user clicks on the button - intent1 method gets called which is defined in the respective class batmanbegins.class

3. TheDarkKnight.class (Second Activity)

TheDarkKnight.class is the second activity presented to the user. As given in the code below, inside the onCreate method, there is a budle object created which is responsible for accepting the incoming intent using getIntent() and getextras() methods. From the intent, which is now referred to by the bundle object, we use getString() method to fetch the data which was present in the intent. This string is dsiplayed through a toast message. Also within the onCreate method, we have an EditText class'object prepared(et) which will be used to accept input from the user.

In this activity also, we will be using an intent object to take the user from this activity to third activity i.e. DarkKnightRises.class
We are going to pick data from the EditText using getText() and convert this text to a String using toString() method. After that the data is loaded onto the intent , like we did in the previous activity using the startActivity() method.

4. Layout for TheDarkKnight.class (Second Activity)

Below is the layout for the second class. We have a TextView,a Button and EditText. EditText,as discussed earlier gets us the users input and by clicking the Button, user is taken to the next activity. Here also, we have used the onClick attribute with the button to call the respective method(intent2() here)

5. TheDarkKnightRises.class (Third Activity)

For the third activity, we follow the same pattern wherein we accept the intent in a bundle object and fetch data from it.The data is displayed in the toast message. Also, when the user clicks on the button in the layout, intent3() method gets called which fires another intent to take the user to the first activity.

6. Layout for TheDarkKnightRises.class (Third Activity)

The below layout is for the third activity. Here we have used a lineat layout with vertical orientation. Also a TextView and a Button are placed inside the layout.

One important thing which is worth mentioning here is that all the intents are fired and a finish() method is also being called with them. This is to ensure that in case the user presses the back button in their phone, they are not taken back to the previous activity instance.

Below are the Screenshots for the output.