DEV Community

Cover image for A light introduction to the Android intent system
Tristan Elliott
Tristan Elliott

Posted on • Updated on

A light introduction to the Android intent system

Introduction

  • This series is going to be dedicated to the basic of Android development. Join me and let us try to build and understand some cool stuff. All the resources I used to create this post can be found on ticketnote or HERE.

Goals of this post

  • By the end of this post I want both you and I to be able to this statment:
Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
Enter fullscreen mode Exit fullscreen mode

What is an Intent in Android?

  • Well as the documentation states, An intent is an abstract description of an operation to be performed. This simply means that anytime you hear or read the word intent you should automatically think to yourself, ok, some sort of work is about to be done. An intent can also be thought of as the glue between activities, as it is often used to share information.

Types of Intents

  • Before we go any further we should clarify the types of intents and what those intents are used for. In Android there are two types of intents:

1) Explicit Intents : this is the kind of intent that has a specified component(class) to be run, often explicit intents will not include any other information. They are a way for an application to launch various internal activities as the user navigates through the app.

  • The intent that we created is actually a explicit intent:
     new Intent(getActivity(), MainActivity.class)
Enter fullscreen mode Exit fullscreen mode

2) Implicit Intents : no component(class) is specified, instead we must provide additional information for the system to determine which of the available components is best to run for that intent.

Building an Intent

  • When building an Intent object we provide it with information and that information usually falls into 1 of 4 categories:

1) The component name : this is literally the name of the component that we want the intent to run. For us it the the MainActivity.class. While the component name is optional, it is a very important piece of information to provide, especially when defining an explicit intent. No component name, no explicit intent. Without a component name the intent becomes implicit and the Android system must use other information(action, data and category) to determine what component is the appropriate one to call.

  • Now I am only talking about the component name because in our example that is only what we use. If you wish to learn more about the intent information then you should read the documentation HERE

Explaining our Example

Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
Enter fullscreen mode Exit fullscreen mode
  • I want to specify that when creating a new Intent, we are using the constructor that requires the current context. By calling getActivity() we get the current activity's context. The MainActivity.class is used to reference the instance of MainActivity that Java stores in memory and we are using it for the component name and making this intent an explicit intent. Lastly we call startActivity(intent), which will use the information stored inside of the intent object to open the correct activity. With that being said we now have a light understanding on what an intent is and what it does.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Oldest comments (0)