Intents

What we’ll cover:

  • PDF / 1,233,626 Bytes
  • 44 Pages / 504 x 720 pts Page_size
  • 0 Downloads / 142 Views

DOWNLOAD

REPORT


Intents What we’ll cover: •

Intent overview



Explicit and implicit Intents



Passing data between activities



Returning results from Intents

Android’s architecture is quite unique in the way it builds application. It has this notion of components instead of just plain objects. And the way that Android makes these components interact is something that can only be found in the Android platform. Android uses Intents as a way for its components to communicate—it uses it to pass messages across components. In this chapter, we’ll look at Intents: what they are and how we use them.

What Intents Are An Intent is “an abstract description of an operation to be performed.1” It’s a uniquely Android concept because no other platform uses the same thing as a means of component activation. In the earlier chapters, we looked at what’s inside an Android application. You might remember that an app is just a bunch of “components” (see Figure 12-1) that are loosely held together, and each component is declared in a manifest file.

https://developer.android.com/reference/android/content/Intent

1

© Ted Hagos 2018 T. Hagos, Learn Android Studio 3 with Kotlin, https://doi.org/10.1007/978-1-4842-3907-0_12

239

Chapter 12

Intents

Figure 12-1.  Logical representation of an Android App What if youl need your components to talk to each other (e.g., launch another Activity)? How do you think we should manage that? If you have any experience with desktop programming, you might do something like the code in Listing 12-1.

Listing 12-1.  Wrong Way to Activate Another Activity class MainActivity : AppCompatActivity {   button.setOnClickListener(object: View.OnClickListener {     override fun onClick(v: View?) {       SecondActivity(). // Won't work    }   }) } class SecondActivity : AppCompatActivity {} Listing 12-1 may appear to be a simple and direct way to launch another Activity, but unfortunately, it’s wrong and it won’t work. An Activity is not a simple object—it’s

240

Chapter 12

Intents

a component. You cannot activate a component just by instantiating it. To launch an Activity, you need to create an Intent object and launch it using the startActivity() function. The code is shown in Listing 12-2.

Listing 12-2.  How to Activate Another Activity button.setOnClickListener {   val intent = Intent(this@MainActivity, SecondActivity::class.java) ➊ ➋   startActivity(intent) ➌ } ➊ this@MainActivity The first parameter of the Intent constructor is a Context object. We passed this@MainActivity because the Activity class is a subclass of Context, so we can use that. Alternatively, we also could have used getApplicationContext(); an Application context would have been accepted just as well. ➋ SecondActivity::class.java. The second parameter is a Class object. It’s the class of the Component to which we want to deliver the message. This a reflection syntax. As you may already know, reflection allows us inspect the structure of our programs during runtime. SecondActivity::class would refer to the runtime reference of SecondAct