Why does Kotlin not have an explicit input

7 Intents I.

Status: 07/28/2020

Mobile apps often consist of different screens that you switch between. It is also common to between different apps to switch, e.g. from a mail in the mail app to the browser to view a link and back, or from a camera app to a chat app to share the photo.

API intent →

Set intents Connections between the activities between activities of the same app and also between activities of different apps. If you want to switch from Activity A to Activity B, you create a new intent object and tells it that you want to call Activity B (a class). Then you pass the intent to an Android method that regulates the transition.

So with a simple transition between two activities we are dealing with these three objects:

Why is supposed to be something supposedly simple like the transition from Activity A to Activity B from one object be managed? Isn't a simple function call enough? No, because intent objects can have a number of Further information included for transition:

  • there are different Types of transitions
  • it can Data passed to the next activity (or a called activity can also return data)
  • it is possible that there is no concrete next activity, but Selection criteria for a next activity

One distinguishes between explicit intents and implicit intents. Explicit intents are used to switch to another activity. So it is a transition within your own app.

We start with explicit intents.

7.2.1 New activity

To do this, we first need a new activity create. In the project view (left) we go up Java and there on our package and choose by Right click: New> Activity> Empty Activity.

Name the new activity "SecondActivity". Android Studio creates a Java file SecondActivity.java and a layout file activity_second.xml.

We imagine an app that has two screens (i.e. two activities). In the first activity (MainActivity) we have a text input and a button. You can write any "message" for the second screen. On the second screen (= SecondActivity) we want to show the text that the user entered on the first screen.

7.2.2 Start the second activity

For the first transition, we create an Intent object that contains both the current Activity () and the target class ():

(That can be problematic - we'll see that in a moment ...)

With the button click we "start" the intent with the method:

In context it looks like this:

As you can see, we had to slightly change this line:

The reason is that we would create an instance of a new (anonymous) class with and would refer to exactly this object. With the following expression we can tell Java in our case that we mean the "parent" object:

Back to the actual topic: Even if we are now in the second activity, the first activity is still "alive". You can use the Back button return to the first activity. You will learn more about the "back stack" in the Lifecycle module.

7.2.3 Provide data via extras

How do we pass on the text that the user has entered? If we still want to transfer data to the second screen, that's what is called Extras.

Extras are structured like one table: we give a key for each piece of information (= string) at. With us the key is called "secret". We use the method to add information to an intent.

So if we had a string, we could with it

add to the intent.

It can look like this in code. Here we get the text from the editable text field (editText). We need the "final" for the intent so that we can access the intent within the button handler.

On the second screen we can get the extra information with us

Alternatively we can write (shorter):

In the code of, it looks like this:

7.2.4 Return with finish

In SecondActivity we simply call the method to return to the MainActivity.

In the code:

When we return, the activity that we are leaving will be irretrievably destroyed.

Of course there are also situations where your second activity calls a third (and this possibly a fourth). But you should No way return to an old activity. You can see why this is so in the "Back-Stack" topic in the Lifecycle module.

There are activities that are used more often as a "service", e.g. pure input screens for names, addresses, etc. Such an activity can therefore be called with the intention that a value (e.g. text) is returned. Then one speaks of one Sub-activity.

7.3.1 Calling the sub-activity

Let's assume we have an activity A that calls a sub-activity B. Again, we use one Intent, but the call is made with.

So that Activity A knows the purpose for which B was called, a RequestCode (a whole number). This is particularly important if A can call several sub-activities, because the results are all collected in a single method.

You can also add extras to your own intent (see above). With this you can give an input mask what exactly you want to query ("Name" or "Telephone").

Let's assume you have a (sub) activity that can ask the user a question and return the answer.

The RequestCode let's just set it to 0.

7.3.2 In the sub-activity

In sub-activity B, for example, an input mask is shown and an input is waited for. Typically you have buttons for "OK" and "Cancel". As soon as OK is pressed, you can go to Activity A jump back. To

  • you create a new intent object and save the relevant information (e.g. text input)
  • sets the result code to RESULT_OK and passes the intent object
  • calls

Note: Do not confuse the result code with the request code! The result code says whether the user canceled or confirmed. The request code tells the calling activity which sub-activity the result comes from.

In our example with the questionActivity, we assume that we are holding an EditText component in the editText variable. If the user confirms the entry, we call this up:

With "Cancel" you set RESULT_CANCELED and call.

Note that after calling finish is completely destroyed (see module lifecycle).

7.3.3 Collect the result

The method is called in activity A after sub-activity B has ended. You have to implement this (i.e. overwrite). Within the method you can look at the ResultCode (is either RESULT_OK or RESULT_CANCELED) and the RequestCode (you have determined yourself). The supplied intent object contains the extras where the input was saved.

In our example, for example, we would be back in MainActivity:

As already mentioned, this one method is always called, regardless of which sub-activity we return from (in the event that more than one sub-activity can be called from A). The Request code is used in such cases to handle the results correctly. Here we have only given the sub-activity a 0 and we get it back.

Intents are objects that the crossing from one activity A to another activity B. A simple transition consists of creating a new intent for activity B in activity A and then calling the method. If you want to return to Activity A from Activity B to return, you just call up.

One can in an intent Store data in the form of a table, the so-called. Extras. These extras can be called up again in target activity B.

An activity B can also be used as a pure "data acquisition" activity with return see. Such an activity could e.g. carry out a user input and return the result - similar to a function. To do this, we call B as Sub-activity with the method on. The calling activity must catch the result with the method.

Contact page with edit activity

Write a sub-activity for a contacts app in which you can enter text.

Your app consists of a page with fields for name, phone and mail:

If one of the three edit buttons is now pressed, the app jumps to a sub-activity in which the text can be entered. Note that above the text input field (class EditText, in the design view under "Plain Text") it is still written which input it is (name, telephone or mail):

If you click on OK, the entry is accepted in the corresponding field:

Here again the process schematically:

Prof. Dr. Michael Kipp
Augsburg University of Applied Sciences
At the university 1
86161 Augsburg
Room J3.18
Tel: +49 821 5586 3509