Kotlin Android Explicit IntentAndroid Intent is a messaging object used to request another app component to perform an action. Intent facilitates users to communicate with app component through several ways such as starting an activity, starting a service, delivering a broadcast receiver, etc. Android intents are mainly used to:
Types of Android IntentsThere are two types of intent in Android: Explicit Intent: This intent satisfies the request within the application component. It takes the fully qualified class name of activities or services that we want to start. Implicit Intent: This intent does not specify the component name. It invokes the component of another app to handle it. Kotlin Android Explicit Intent ExampleIn this example, we will call the other activity class from another activity class using explicit intent. Using intent, we will send the data from the first activity class to second activity class. Second activity class gets this data and displays them in toast message. activity_main.xmlAdd the following code in the activity_main.xml. MainActivity.ktAdd the following code in the MainActivity.kt class. In this class, we are creating an instance of Intent class and calling the component activity class SecondActivity.kt. The putExtra(key, value) method of Intent class send the data to the SecondActivity.kt class. The startActivity() method starts the Intent. Create another activity class named as SecondActivity. second_activity.xmlIn the second_activity.xml file add the following code. SecondActivity.ktAdd the following code in the SecondActivity.kt class. In this class, we are receiving the intent data using creating the instance on Bundle class using intent.extras and displaying the data in toast message. By clicking on the button, we are invoking Intent to call MainActivity.kt class. Output:
Next Topic#
|