Passing Values Between Two Intents

Passing Values Between Two Intents

android, TUTORIALS

Passing Values Between Two Intents




In this tutorial Passing Values Between Two Intents we are gonna learn how to passing the values between two intents when we switch from one intent to another. For this tutorial take our previous tutorial Switching between intents.
From the previous tutorial we are gonna change little bit changes to achieve current task. Here we use the same application from our last tutorial, so just open it in android studio. And follow the given instructions to passing the values form one intent to another intent.
Step 1: Add the TextEdit component in activity_css.xml
Before adding a component we should import the related namespaces at top of the file.
Download Project File!!!
In this tutorial Passing Values Between Two Intents we are gonna as a first step go to the activity_css.xml the main activity or the launcher screen. Set the id for this TextEdit component as textMessage. And the screen is looks like this,
Passing Values Between Two Intents
Step 2: Add the TextEdit component in activity_css.xml
And in the next intent we need to create a space to receive the content from the previous intent. We can get any kind of data from the other intents, but here we got the text values so here we use the TextView Component. And the result is looks like this,
Passing Values Between Two Intents
In the above shown image we have the TextView component with the default placeholder text like Your Message is Shown Here… The Data whatever comes from the css activity is shown at here.
Ok, Still know the designing part has been completed and lets now its time for the logical part, and see how they are works.
Step 3: Add Code to css.java
Add the following code instead of the previous code in the css.java file.

At the second line after initiate the intent, we have to define the EditText with its id. Like we did for our previous components.
And then we get the values from the EditText component whatever we typed and convert the text in to string using the toString() function. After convert it to the string data type then store the values into the variable userMessage.
Here we Passing Values Between Two intents implement need to transfer the values to the next intent. So we use the method called putExtra() to take care of this work. In this putExtra() pass the values in the form of key value pair.




We pass the two parameters, first one is value how we want to call the key and the second parameter is the value what we we wants to transfer from one intent to another intent.
Step 4: Add Code to html.java
Now we have to ready for getting the values form the previous or the another intent which is switching by a button click or any other methods.
Add the following code into your html.java inside the protected void onCreate(Bundle savedInstanceState) function.

The above code is added in the html.java and here we’ve to ready for receive the content or parameter. So we here use the Bundle function to get the extra values and intents.
After receive the content we are check whether content is null are not. If the content is not null then we have to save the received data in a string variable.
After receive the text and now set the text on the TextView component where the value is stored in cssMessage. And that’s it everything is gonna fine, now execute the application through our Emulator or any other private emulators like Genymotion, Andy, or Bluestack.
Passing Values Between Two Intents
We hope this lesson will helps you a lot about Intents in Android and passing the values between the intents.




Leave a Reply