About This Series
This “Android Kotlin Basics” blog series is all about fundamentals. We’ll take a look at the basics of building Android apps with Kotlin from the SUPER basics, to the standard basics, to the not-so-basics. We’ll also be drawing comparisons to how things are done in Kotlin vs. Java and some other programming languages to build Android apps (like C# and Xamarin or JavaScript/TypeScript for Hybrid implementations).
Check Out the Pluralsight Course!
If you like this series, be sure to check out my course on Pluralsight – Building Android Apps with Kotlin: Getting Started where you can learn more while building your own real-world application in Kotlin along the way. You can also join the conversation and test your knowledge throughout the course with learning checks through each module!
Watch it here: https://app.pluralsight.com/library/courses/building-android-apps-kotlin-getting-started/table-of-contents
Passing Data Between Activities
This topic is covered in depth in the course above, but in this post, we’ll quickly go over this basic, but very important concept when developing your Android mobile apps.
Let’s break it down first –
I Have No Idea What an “Activity” Is
Not to worry! Here’s the basic idea – it’s a construct (and a class in your code) that represents an entry point for your app and can be called into by your other Activities. You can think of an Activity as a contextual “page”, and thus your app will likely have multiple Activities.
Activities can do a lot, but for simplicity sake, we’ll only talk about the most common implementation which is this idea of a drawing a window of your application.
Starting an Activity
When you create your application, you’ll have to register your Activities to your Android manifest. Doing this tells the operating system which Activities you have in your app, where to find them, what they can do, and some other information we won’t get into in this topic.
That looks something like this:
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.alexdunn.wikipedia"> <uses-permission android:name="android.permission.INTERNET"/> <application android:name=".WikiApplication" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme.NoActionBar"> <activity android:name=".activities.MainActivity" android:label="@string/title_activity_main"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".activities.ArticleDetailActivity"></activity> <activity android:name=".activities.SearchActivity"></activity> </application> </manifest>
This is a manifest with 3 Activities, MainActivity
, SearchActivity
, and ArticleDetailActivity
.
Once all Activities are registered in the Manifest, we can start any of them from any other.
Let’s look at a quick example where we are in the MainActivity
and want to start the SearchActivity
on a button click:
MainActivity.kt
class MainActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { my_button.setOnClickListener { // create an "Intent" val searchIntent = Intent(this, SearchActivity::class.java) // start the activity with the intent startActivity(searchIntent) } } }
So what’s this Intent
object?
An Intent
in Android is a construct to tell the operating system that your application is trying to execute something that needs to be communicated back to the application or a different application. This is used for many things outside just starting Activities – it can be used for executing Services, Activities, and BroadcastReceivers.
Here’s a quick diagram on how this works at a basic level:
Adding Data to an Intent
So now we know we use Intents
to start Activities (and do other things!), but how do we use them to actually send data to another Activity?
The Intent
object has a the concept of a Extras
. This means we can add different primitive datatypes to the Extras
, or add Serializable
data.
Here’s what that looks like in Kotlin!
// 1. create an intent from another activity val intent = Intent(context, AnotherActivity::class.java) // 2. put key/value data intent.putExtra("message", "Hello From MainActivity") // 3. start the activity with the intent context.startActivity(intent)
The putExtra
has many overloads, so play around with all the types of data you can add.
You can also add Bundles
of data to group the data being sent up together:
// 1. create an intent from another activity val intent = Intent(context, AnotherActivity::class.java) // 2. put key/value data to bundle val extras = Bundle() extras.putString(“message”, “Hello from MainActivity”) intent.putExtras(extras) // 3. start the activity with the intent context.startActivity(intent)
Reading Data From the Intent
Now when we use an intent to start an Activity, and add Extras
, we need to be able to read those extras on the new Activity.
This works by using the intent
property on the Activity
. This intent
property is set by the Operating System when starting the Activity, and sets it to the Intent
that created it.
We usually use this data in the onCreate
override, but you can use it wherever you want!
DetailActivity.kt
class DetailActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { // In onCreate get the value from the auto-mapped “intent” val message = intent.getStringExtra(“message”) // Or get it from the bundle and auto-mapped “extras” val message = intent.extras.getString(“message”) // now do something with the message value } }
Conclusion
There’s plenty more you can do with these data structures in Android such as sending over full datatypes and reference types, receiving data back from the detail Activity, and so much more. Be sure to check out my course: Building Android Apps with Kotlin: Getting Started to learn so much more!
Also, let me know what else you’d like to learn about with Android and Kotlin! Either drop a comment here or tweet at me @Suave_Pirate!
If you like what you see, don’t forget to follow me on twitter @Suave_Pirate, check out my GitHub, and subscribe to my blog to learn more mobile developer tips and tricks!
Interested in sponsoring developer content? Message @Suave_Pirate on twitter for details.