Lesson 5. Application Lifecycle and basic navigation
This lesson explains the activity lifecycle for an Android application using Kotlin.
Activity Type | Lesson |
---|---|
Expected Duration | 30 minutes |
Topic | Application Lifecycle and basic navigation |
Objective | Get to know Android's lifecycle paradigm and navigation concepts while using Kotlin and Anko. |
1. Lifecycle Concepts
Activities in Android provide a core set of six basic callbacks to navigate transitions between its stages: onCreate(), onStart(), onResume(), onPausa(), onStop(), and onDestroy(). This figure sums up how they're called and excecuted:
If you want to know more about these callbacks (and you should) please visit The Activity Lifecycle
2. Activity state and ejection from memory
As seen on The Activity Lifecycle
Android's system kills processes when it need RAM, and the likelihood of this is given by the state of the process at the time:
Likelihood of being killed | Process state | Activity state |
---|---|---|
Least | Foreground | Created, Started, Resumed |
More | Background (lost focus) | Paused |
Most | Background (not visible), Empty | Stopped, Destroyed |
If you want to know more about Processess, read Process Lifecycle
3. Navigating between Activities
When developing an app, you are likely to build several activities and connect them, perhaps multiple times, during your app's lifecycle. If you where to use basic Kotlin it would be virtually the same as in Android, but there's a library called Anko, which will make your life easier. Find it here and follow the instructions in lesson 1 to install it. Now, let's take a look at some ways it can improve your code:
- Inten builders:
In general, it takes you a couple of lines to start a new Activity and pass over some flags or data:
val sendIntent = Intent().apply {
action = Intent.ACTION_SEND
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, "moviles2020")
}
startActivity(sendIntent)
But with Anko and Kotlin, this is too much, there's an easier way:
startActivity(intentFor<NextActivity>(PARAM1 to "moviles2020", ...))
Now, you may not need to pass any flags, and Anko makes it even easier:
startActivity<NextActivity>(PARAM1 to "moviles2020", ...)
Passing data through intents restricts the size of your messages, and therefore sending objects is only encouraged when Parcelizing them. Read more about Parcels here.
There are even some pre-defined intents that will make it increadibly easier:
Goal | Solution |
---|---|
Make a Call | makeCall(number) |
Send a Text | sendSMS(number, [text]) |
Browse the web | browse(url) |
Share some text | share(text, [subject]) |
Send an email | email(email, [subject], [text]) |
Arguments in [] are optional.
- startActivityForResult()
Sometimes you do want to get a result back from an activity after it's excecution, and this can be done by using startActivityForResult() This is an example of an Activity that allows the user to select a contact
const val PICK_CONTACT_REQUEST = 1 // The request code
...
private fun pickContact() {
Intent(Intent.ACTION_PICK, Uri.parse("content://contacts")).also { pickContactIntent ->
pickContactIntent.type = Phone.CONTENT_TYPE // Show user only contacts w/ phone numbers
startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST)
}
}
And here's how you can handle the result for the "pick a contact" intent:
...
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {
// Check which request we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {
// Make sure the request was successful
if (resultCode == Activity.RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}
If you want to know more about get info between activivities, read Getting a Result from an Activity
- onBackButtonPressed()
Have you noticed that every Android phone comes either with a physicall or virtual back button? Well, you can take advantage of it by overriding it's callback on the current activity. Calling super() on the callback will lead you to the last active activity and call finish() on the current one.
3. Coordinating activities
When one activity starts another one, both experience lifecycle changes:
Activity | Method called | State |
---|---|---|
Activity A | startActivity(B) | calls onPause() |
Activity B | onCreate() | calls onStart() |
Activity B | onStart() | calls onResume() |
Activity A | onStop() | eventually calls onStop() and onDestroy() |
If you go back from B to A:
Activity | Method called | State |
---|---|---|
Activity A | startActivity(B) | calls onPause() |
Activity B | onCreate() | calls onStart() |
Activity B | onStart() | calls onResume() |
Activity B | calls finish() | calls onPause() and onStop() |
Activity A | onResume() | Activity Running |
Version | Author | Date |
---|---|---|
1.0 | Juan Santiago Acevedo | February 14, 2018 |
2.0 | Vivian Gómez Cubillos | February 12, 2020 |