Activity, Intent, Bundle

An Android app could contain one or many activities. An activity has its own lifecycle and we can launch a new activity by using intents. In order to send data from an activity to another we could use a Bundle.

Activity

Activity lifecycle

  1. onCreate() :
    1. When: it is called when an activity is first created
    2. What: all the necessary UI elements should be initialized
    3. Is activity visible: not yet
  2. onStart():
    1. When: an activity becomes visible to the user
    2. What: code that maintains the UI, start async tasks (get data from API or database), register listeners
    3. Is activity visible: yes
  3. onResume():
    1. When: is called before the user starts interacting with the activity
    2. What: start animations
    3. Is activity visible: yes
  4. onPause():
    1. When: user is leaving the activity
    2. What: stop animations
    3. Is activity visible: partially invisible
  5. onStop():
    1. When: the activity is no longer visible to the user
    2. What:  unregister listeners and all resources allocated in onStart()
    3. Is activity visible: no
  6. onRestart():
    1. When: the activity in the stopped state is about to start again (on back click)
    2. What: the cursor should be requeried
    3. Is activity visible: no
  7. onDestroy(): 
    1. When: the activity is destroyed from the memory
    2. What:  should never be overridden
    3. Is activity visible: no

To get data back from a launched activity, start that activity with the startActivityForResult() method instead of startActivity().

Let’s say we have 2 activities: Activity1 and Activity2 and we want to validate the message sent from Activity1 to Activity2 and just get the result after validation.

Steps to implement this behaviour:

  1. Activity1 => startActivityForResult() applied to an Intent with putExtra containing a String
  2. Activity2 => get the String from the bundle and validate data
  3. Activity2 => setResult() and finish current activity
  4. Activity1 => get the result from Activity2 by overriding onActivityResult()

The response codes are defined by the Activity class, and can be

  • RESULT_OK: The request was successful.
  • RESULT_CANCELED: The user canceled the operation.
  • RESULT_FIRST_USER: For defining your own result codes.

Official documentation:

Demo:

Code sample:

Intent

  • An Intent is a messaging object you can use to request an action from another app component
  • Although intents facilitate communication between components in several ways.

Intent types

Explicit Intent 

Starts a specific activity: an activity created by the developer 

Implicit Intent 

Asks system to find an activity that can handle the request: an activity created in the system (call, share, display a website, etc) 

Bundle

Used to transfer data using Intent.

Resources

One thought on “Activity, Intent, Bundle

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s