Intent

Intent

Introduction:

  • In Android, it is quite usual for users to witness a jump from one application to another as a part of the whole process, for example, searching for a location on the browser and witnessing a direct jump into Google Maps or receiving payment links in Messages Application (SMS) and on clicking jumping to PayPal or GPay (Google Pay).
  • This process of taking users from one application to another is achieved by passing the Intent to the system. Intents, in general, are used for navigating among various activities within the same application, but note, is not limited to one single application,
  • i.e., they can be utilized from moving from one application to another as well.
  • Intents could be Implicit, for instance, calling intended actions, and explicit as well, such as opening another activity after some operations like onClick or anything else.
  • Below are some applications of Intents:
  1. Sending the User to Another App
  1. Getting a Result from an Activity
  1. Allowing Other Apps to Start Your Activity
  • The collaborative nature of Android applications only results in a better user experience. The question here is if the intent is for an application that is not present in the device, what’s the next call?

Some Important Method of Intent and their Description

Methods
Description
Context.startActivity()
This is to launch a new activity or get an existing activity to be action.
Context.startService()
This is to start a new service or deliver instructions for an existing service.
Context.sendBroadcast()
This is to deliver the message to broadcast receivers.

Deep Linking

  • Deep Link is an URL that redirects the device to the API of that Missing Application and then the service is run on the system to check if a version of that application exists on the device.
  • For time being, let’s assume that the application is not available on the device and no previous versions ever existed.
  • This service then makes a call to the Play Store from the device and the application appears, just a matter of download.
 
  1. Implicit
  1. Explicit
 
Android 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:
  • Start the service
  • Launch an activity
  • Display a web page
  • Display a list of contacts
  • Broadcast a message
  • Dial a phone call etc.

Types of Android Intents

There are two types of intents 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.
val intent = Intent(applicationContext, SecondActivity::class.java) startActivity(intent)
// Button to go to next activity button2.setOnClickListener(){ intent = Intent(this@MainActivity,SecondActivity::class.java) startActivity(intent) }
Implicit Intent: This intent does not specify the component name. It invokes the component of another app to handle it.
intent = Intent(Intent.ACTION_VIEW) intent.setData(Uri.parse("https://www.javatpoint.com/")) startActivity(intent) // or (Same) intent= Intent(Intent.ACTION_VIEW, Uri.parse("https://www.javatpoint.com/")) startActivity(intent)
 
 

PutExtra()

  • this is implemented before we startActivity() function called
  • it is used to take / provide data to intended activity
  • it takes data in key - value format
  • Any Premitive data type can be passed with this method
  • and for object of Data Class / Class to be passed it need to be Extended with Serializable class
  • to get value/data from intent each datatype has its unique method

From Above example

val intent = Intent(applicationContext, SecondActivity::class.java) // putExtra("key","value") intent.putExtra("username" , username.text.toString()) startActivity(intent)
 
To get Particular type of data from intent
intent.getIntExtra() intent.getStringExtra() intent.getBooleanExtra() intent.getDoubleExtra() intent.getStringArrayListExtra() intent.getBooleanArrayExtra() intent.getBundleExtra() // etc.. // each take key as argument to get value // and default value if any data didnot get

send and recive object with intent.putExtra()

To Put/Pass Object with putExtra()
// Here prod1 is Object of data class Product which was extended with Serializable class val intent = Intent(showProduct,ProductDetails::class.java) intent.putExtra("product", prod1) startActivity(intent)
 
To get Serializable object from intent
// after getting value it need to be cast to class object val product = intent.getSerializableExtra("product") as Product