A Light Introduction to the Android Intent System


  • This series is going to be dedicated to the core of Android development. Join me and let’s try to make 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 understand this statement:
Intent intent = new Intent(getActivity(), MainActivity.class);
        startActivity(intent);
enter fullscreen mode

exit fullscreen mode

What is an intent in Android?

  • Also the documentation states, An intent is an abstract description of an operation to be performed. It simply means that whenever you hear or read the word intent You should think to yourself, okay, something is going to happen. An intent can also be thought of as the glue between activities, as it is often used to share information.

types of intentions

  • Before we go any further, we should clarify what types of intents and what those intents are used for. There are two types of intents in Android:

1) clear intentions : This is the kind of intent that a specified component (class) is intended to run, often with no other information included in the explicit intent. They are a way for the application to launch various internal activities as the user navigates through the app.

  • What we have created is actually a clear intention:
     new Intent(getActivity(), MainActivity.class)
enter fullscreen mode

exit fullscreen mode

2) implied intentions : No component (class) specified, instead we must provide additional information for the system to determine which available component is best running for that intent.

make an intention

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

1) Component name : This is literally the name of the component we want to run. it for us 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 ingredient names, no clear intent. Intent without a component name becomes implicit And the Android system must use other information (action, data and category) to determine which component is appropriate to call.

  • Now I’m only talking about component name Because in our example that’s the only one we use. If you want to know more about intent information you should read the document here

Explanation of 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 which requires the current context. By calling getActivity(), we get a reference to the current activity. MainActivity.class is used to refer the instance of MainActivity which Java stores in memory and we are using it for component name And making this intention a clear intention. finally we call startActivity(intent), which will use the information stored inside the Intent object to open the correct Activity. With that being said, we now have a lighter understanding of what an intent is and what it does.
  • 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 contact me Twitter.

.



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *