Lesson 3. Application Lifecycle and basic navigation

This lesson explains the activity lifecycle for an Android application.

Activity Type Lesson
Expected Duration 30 minutes
Topic Application Lifecycle and basic navigation
Objective Get to know Android's lifecycle paradigm and navigation concepts.

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 https://developer.android.com/guide/components/activities/activity-lifecycle.html

4. 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. Here are some useful methods to accomplish that while keeping the lifecycle clean and neat.

  • startActivity()

If you want to start a new activity, and do not wish to get a result from it, you can simply launch it by using the startActivity() method:

  // Create the text message with a string
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("text/plain");
    sendIntent.putExtra(Intent.EXTRA_TEXT, "moviles2020");
    // Start the activity
    startActivity(sendIntent);

And then you can get the data after the onCreate() callback in the second activity by using:

getIntent().getStringExtra(PARAM1)

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.

  • 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

static final int PICK_CONTACT_REQUEST = 1; // The request code.
// ...
private void pickContact() {
    Intent pickContactIntent = new Intent(Intent.ACTION_PICK);

    // Show user only the contacts that include phone numbers.
    pickContactIntent.setDataAndType(Uri.parse("content://contacts"),
            Phone.CONTENT_TYPE);
    startActivityForResult(pickContactIntent, PICK_CONTACT_REQUEST);
}

And here's how you can handle the result for the "pick a contact" intent:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == PICK_CONTACT_REQUEST) {
        // Make sure the request was successful
        if (resultCode == 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

results matching ""

    No results matching ""