Android Internals - Part 2

NeelNeel
3 min read

Activity class provides a number of callback methods, that let an activity know when the state changes or the system is going to stop/create/resume/destroy the activity or going to stop/create/resume/destroy the process that activity resides in.

Within the life cycle callback methods you can define how the activity should behave for that particular state.

Source - https://developer.android.com/guide/components/activities/activity-lifecycle?hl=en

There are 6 major callbacks.

  1. onCreate()

    Fires when the system first creates the activity.

    State - CREATED

    You can write basic start up logic that will happen only once during the lifecycle of the activity.

    Basic setup of views/data/UI

    This callback receives an input parameter savedInstanceState which is a Bundle object containing Activity’s previous state.If the activity is being created for the first time then this param would be null.

  2. onStart()

    This call makes the activity visible to user as the app prepares the activity to enter foreground and become interactive.

    State - STARTED

    Use - Start animations

  3. onResume()

    This is the state where activity comes on to foreground. The app interacts with the user.

    The app stays in this state until something happens to take the focus away, eg: phone call or user navigates to different activity etc.

    State - RESUMED

    Use - run any functionality that requires app to be in foreground and visible

  4. onPause()

    This is the first indication that user is leaving activity.However it does not always imply that activity is going to be destroyed. This indicates that activity is no longer in foreground, but it woould still be visible in multi-window mode.

    Use - Release of system resources like GPS, Camera

    As mentioned earlier a paused activity might be fully visible in multi-window mode, consider using onStop() method to fully release system resources or adjust UI resources to better support multi-window mode.

    onPause() execution is very brief and does not necessarily offer enough time to perform save operations.

    Completion of the onPause() method does not mean that the activity leaves the Paused state. Rather, the activity remains in this state until either the activity resumes or it becomes completely invisible to the user. If the activity resumes, the system once again invokes the onResume() callback.

  5. onStop()

    When your activity is no longer visible on screen,it enters stopped state.

    State - Stopped

    This happens when a newly launched activity completely covers the screen. The system also calls onStop when the activity finishes running.

    Use - stop any functionality that does not need to run while the component is not visible on the screen.

    When your activity enters the Stopped state, the Activity object is kept resident in memory: it maintains all state and member information, but is not attached to the window manager. When the activity resumes, it recalls this information.

  6. onDestroy()

    This callback is called before the activity is destroyed.

    This is called for two reasons:

    1. User dismisses the activity or finish() is called on activity

    2. system is destroying the activity temporarily due to config change (eg: device rotation/entering multi-window mode)

0
Subscribe to my newsletter

Read articles from Neel directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Neel
Neel