All About Intents

Glitch
4 min readJun 14, 2021

--

An Intent is a messaging object you can use to request an action from another app component.

In simple words, it is used to communicate between two activities fragment, etc.

Intent contains the following things:

  • Component name
  • Action
  • Data
  • Category
  • Extras
  • Flags

Here, the detailed explanation about Intent objects

  • Component name — This information is optional. In case the component name is set, the Android system directly maps the intent to the target component. Else, the system utilizes other information to locate the suitable target. The component name is set by setComponent (), setClass () and is read by getComponent ().
  • Action — This is mandatory part of information names the action an Android component should take on receiving the Intent , or is being notified to the system and the action that has already taken place. The Android system defines a number of Action Constants

Here, We are listed two

ACTION_BATTERY_LOW — Battery low notification

ACTION_CALL — Initiate a phone call

  • Data: Contains the Uniform Resource Identifier (URI) of the data. There are certain types of data specifications for different actions. For instance, if the action defined were ACTION_CALL, the data field would contain a number to call (tel: URI). For correctly matching the Intent to a suitable component and defining the data type (in addition to the URI) helps. And, a component to display image should be called to display image only, not to play an audio file.
  • Category: This category is optional part and it defines the category of the component that would handle by the Intent.
  • Extras: This is the key value pair for additional information that needs delivered to the component handling the intent.
  • Flags : Flags are defined in the Intent class that function as metadata for the intent and The flays may instruct the Android system to launch an activity in a specific manner.

Three fundamental use cases:

Starting an activity

Starting a service

Starting a service

Intent types

Explicit Intent

It going to connect the internal world of an application such as start activity or send data between two activities.

The code snippet of code above is an example of explicit intent. Have a look at it again.

Intent numbersIntent = new Intent(MainActivity.this, NumbersActivity.class);

startActivity(numbersIntent);

Implicit Intent

It going to connect with outside applications such as calls, mail, phone, see any website ..etc.

Here’s an implicit intent:

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
startActivity(intent);

3. Pending Intent:

Intent notifyIntent = new Intent(this, ResultActivity.class);
// Set the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
);NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());

This is the most common use case for PendingIntent. When you want to open some application components like Activity/Service/BroadcastReceiver at later time or after some specified time interval you have to send PendingIntent in this case. It acts like a permission slip you gave to another apps to run your application’s code on your behalf after some time. So PendingIntent works beyond process boundaries like you want AlarmManager which is an another app in another process then AlarmManager perfom action on your app specified by PendingIntent. So this type of intent is called PendingIntent.

4. Sticky Intent(Sticks around):

IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
Intent batteryStatus = context.registerReceiver(null, ifilter);

This works in conjunction with sendStickyBroadcast() method. So when android system send sticky broadcast for any change in battery status the Intent stick around even after the BroadcastReceiver has completed it’s work. So at later point of time anyone can retrieve that Intent for getting the battery status result through registerReceiver() method. So this type of intent is called Sticky Intent.

Passing values using intent:

  1. PutExtra
Intent submitIntent = new Intent(MainActivity.this, ShowActivity.class);
submitIntent.putExtra("firstNameString", firstNameString);
submitIntent.putExtra("lastNameString", lastNameString);
startActivity(submitIntent);

2. Parsing data using bundles

// Creating Bundle object
Bundle b = new Bundle();

// Storing data into bundle
b.putString("firstNameString", firstNameString);
b.putString("lastNameString", lastNameString);

// Creating Intent object
Intent submitIntent = new Intent(MainActivity.this, ShowActivity.class);

// Storing bundle object into intent
submitIntent.putExtra(b);
startActivity(submitIntent);

3.Parcellable

Parcelable the process is much faster than Serializable. One of the reasons for this is that we are being explicit about the serialization process instead of using reflection to infer it. It also stands to reason that the code has been heavily optimized for this purpose.

// Array of MyObjects
ArrayList<MyObjects> mUsers;

// Passing MyOjects instance
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putParcelableArrayListExtra("UniqueKey", mUsers);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
ArrayList<MyObjects> mUsers = mIntent.getParcelableArrayList("UniqueKey");

4.Serializable

Serializable is a standard Java interface. You can just implement Serializable the interface and add override methods. The problem with this approach is that reflection is used and it is a slow process. This method creates a lot of temporary objects and causes quite a bit of garbage collection. However, Serializable the interface is easier to implement.

// MyObjects instance
MyObjects mObjects = new MyObjects("name", "age", "Address array here");

// Passing MyObjects instance via intent
Intent mIntent = new Intent(FromActivity.this, ToActivity.class);
mIntent.putExtra("UniqueKey", mObjects);
startActivity(mIntent);
// Getting MyObjects instance
Intent mIntent = getIntent();
MyObjects workorder = (MyObjects) mIntent.getSerializableExtra("UniqueKey");

That’s it .that was a long one and a bit more theoretical but trust me it is something you will use everywhere mostly. So keep coding!!

--

--

No responses yet