Learn Android — Multi-screen Applications

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
5 min readJun 11, 2018

--

Welcome to the Learn Android series. I’m going to share with you, regularly, why and how to start with Android and build your portfolio. I’ll share with you explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learned how to make our applications respond to user actions like clicking a Button and how to dynamically change content of TextView’s and reading user inputs from EditText’s and more. In this article, we will learn about how to navigate from one activity to another and how to launch external applications that are able to perform a specific task.

Previous Article: Learn Android — Adding Interaction

We will be telling Android how to move between different activities in our app and how to pass data between them. So far we’ve developed everything on a single screen. So, how do we start an activity from another?

We use Intents.

Intents are messaging objects which facilitates communication. These can be used to start a new activity or to use any external app to perform a particular function like going to the dialler, picking up an image from the gallery or even switching on the camera.

Intents are also able to carry data with themselves, but they can only carry information which are in the form of primitive data types. Before we learn how to use intent, we’ll learn about it’s types. There are majorly 2 types of intents — Implicit Intent and Explicit Intent.

Explicit Intents are specific intents and these are the one that we use to start another activity in our applications. These types have a specific destination and we need to supply the target app’s package name or the target class name. And these are mostly used inside the application.

Implicit Intents are general and do not have any specific target app. But the major advantage of these are, showing the users all the available options and letting them choose for themselves. For example, if you want to let users contact you via email, you can add an implicit intent and let them choose the email client (Gmail, Inbox, Outlook, etc.) the users are comfortable with.

Talking about in-app intents first, to start an activity from a current one, startActivity() function is called with two parameters — Current Context and Destination Activity’s Class name. Let’s see an example —

In the above given code, we create an Intent object and initialize with the two required parameters. Then we use the startActivity() function and pass in the intent object to start the target activity. But we just should not directly declare this in the onCreate() of the activity. For example, let’s say there are 3 activities — A, B, and C. If we want to go from A to B to C, and we need to see each one of those, we should start the activities on some user event. Because if we call the startActivity() function directly in the onCreate() methods of all the activities, then the user will not seen the transition from A → B → C, but rather only from A → C directly. So, these are mostly carried out on a button click for example. The code will be similar but let us see another example of starting another activity when a user clicks on a button.

We can also pass in the data along with an intent in the following way —

This passes the value of the number variable into another activity, which in our case is the TargetActivity and we can retrieve it using the TAG which is “NUMBER” in our case.
To retrieve it we can use —

Moving on to implicit intents, let’s take an example of allowing users to send an email to the application developer —

Here, the putExtra() methods accepts in two parameters — the TAG and the data. The TAG will help it classify the type of information that is received. Furthermore, the setType() method is useful for the Android operating system to determine which apps to include in the chooser dialog that is shown to the users with the apps able to perform the required tasks. The Intent.EXTRA_EMAIL, Intent.EXTRA_SUBJECT or Intent.EXTRA_TEXT tags are predefined for sending an email.

Let’s take another example of an implicit email where we include a link into the data that intents carry and we intend to open it in any browser available on the user’s phone —

This code will open google.com in the web browser. We can substitute any URL and that will be handled by the Android Operating System to open it in either the default browser or if default not specified, the list of available browsers installed in the user’s phone will be shown.

We’ll talk more about receiving intents from another applications and some more about intents in the near future. But if you are too inquisitive, you can read about it at the Android Documentation here!

Next up: Learn Android — Custom Data Holders

Stay tuned for regular updates. Follow me and Parsed Inc. to never miss another one!

Also, let’s become friends on LinkedIn, GitHub, Twitter and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--