What are intents in Android?
Let's see today what exactly is intents in android. According to Google, an Intent is a messaging object you can use to request an action from another app component.
So, What does it mean?
Let's dive little further into the topic and see what are the different types of Intents we have in Android and how to use them.
Different types of Intents:
One of the usages of Intents is to Start an Activity(If you don't know what is an Activity in Android please read my article here). Let's see how to do it.
There are two types of Intents in Android.
- Explicit Intents:
In this type of Intents, we know the Exact Activity that needs to respond.
Confused!
Don't worry I'll explain with an example.
Let's consider we are doing an app with two Activities namely Activity1.java and Activity2.java
On a button click, we need to move from activity1 to activity2. So, here we mention activity2 to respond to the intent.
How do we do it? Let's see an example
Here's the first Activity which is also the launcher activity.
This layout consists of two components
- EditText: Used to take input from the user
- Button: To set a click action
In this example what we are doing is taking some input from the user into editText and clicking on button takes you to the next activity and we display the value written in EditText in the next activity.
set on click listener for the button and inside it take the input value from editText and save it in a string variable data.
Now create an object for Intent and the constructor will take two parameters, the first parameter is current activity name and the second parameter is the target activity that should be responded to the intent.
In the put extra method, we give two parameters, first is the key and the second parameter is the value that we want to send. The value captured in the above string is sent to the second activity. In our example "vanitha" is the key and the value is the editText value captured in a String variable
set on click listener for the button and inside it take the input value from editText and save it in a string variable data.
Now create an object for Intent and the constructor will take two parameters, the first parameter is current activity name and the second parameter is the target activity that should be responded to the intent.
In the put extra method, we give two parameters, first is the key and the second parameter is the value that we want to send. The value captured in the above string is sent to the second activity. In our example "vanitha" is the key and the value is the editText value captured in a String variable
Now here's the second activity where we capture the data coming from the first activity using getextras() method and the getextras method take the key that is given in the put extras method which in our case is "vanitha" and save it in a string variable.
Now set this string variable to textview using method as shown in the below code.
So this is one of the types of Intents in Android and also the sending the data from one activity to another activity.
In the next article, I'll write about another use case of explicit intents and the other type of intents which is Implicit intents.
Hope you enjoyed this article!!
Keep coding!!!😊😊
Follow @durgasandroid
Tweet
Comments
Post a Comment