Android - (Activity|Fragment) Lifecycle

Card Puncher Data Processing

About

The Activity lifecycle is implemented by callback methods.

A good way to test the lifecycle implementation (ie the application's ability to restore its state) is to simply rotate the device so that the screen orientation changes.

Android Activity Lifecyle Activity Lifecycle 2

States

State Only for Fragment Callback Function Visible by the user 1) Foreground (Focus) Alive 2)
Attached Yes onAttach No No ?
Created - onCreate No No ?
View Created Yes onCreateView No No ?
Activity Created Yes onActivityCreated No No ?
Visible onStart Yes No ?
Resumed (or Running, Active) - onResume Yes (Foreground) Yes Yes
Paused - onPause Yes (Partially) No Yes
Stopped - onStop No (Background) No Yes
View Destroyed Yes onDestroyView No (Background) No No ?
Destroyed - onDestroy No (Background) No No ?
Detached Yes onDetach No (Background) No No ?
Restart - onRestart No No ?

An application can be killed by the system in extremely low memory situations. It can drop it from memory either by:

  • asking it to finish (calling its finish() method),
  • or simply killing its process.

You should prepare for app death as early as onPause (ie closing resources such as connection or sockets). The function onSaveInstanceState is called before an activity may be killed. It gives a chance to save the current state and to restore it via the function onRestoreInstanceState.

Skeleton

The following skeleton activity includes each of the fundamental lifecycle methods:

public class ExampleActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
        //    * Setup of "global" state (such as defining layout, loading layout file, ...)
        //    * Background Thread
    }
    @Override
    protected void onStart() {
        super.onStart();
        // The activity is about to become visible.
        // The system might call onStart() and onStop() multiple times during the entire lifetime of the activity, 
        // as the activity alternates between being visible and hidden to the user.
        //      * register a BroadcastReceiver to monitor changes that impact your UI, 
    }
    @Override
    protected void onResume() {
        super.onResume();
        // The activity has become visible (it is now "resumed").
        // The foreground lifetime of an activity happens between the call to onResume() and the call to onPause(). 
       // Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.
    }
    @Override
    protected void onPause() {
        super.onPause();
        // Another activity is taking focus (this activity is about to be "paused").
        //  For example, onPause() is called when the device goes to sleep or when a dialog appears. 
        // The activity is still visible !
        //
        // You should prepare for app death (ie closing resources such as connection or sockets)
        // Example also available for the onStop steps:
        //    * Sensor Listener
        //    * Location Updates
        //    * Dynamic Broadcast Receivers
        //    * Game Physics engine

    }
    @Override
    protected void onStop() {
        super.onStop();
        // The activity is no longer visible (it is now "stopped")
        //      * unregister a broadcast receiver
        // Must be considered as an exit handler (all resources must be released). See onPause
        // Background app  shouldn't consume resources
        
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // The activity is about to be destroyed.
        //     * Release all remaining resources such as a background thread
    }
}

List

Active

  • Active: foreground and in focus
  • Visible: start of an extern activity, visible on background

Android Lifecycle Active

Documentation / Reference

1)
The user can see the activity on-screen and interact with it
2)
the Activity object is retained in memory, it maintains all state and member information, but is not attached to the window manager





Discover More
Android Studio Activity
Android - Activity (UI Single Screen)

An activity is a plain Java class representing a single screen (known also as a presentation layer). Every Activity is invoked by an Intent, regardless of how the user navigated there. You can get the...
Card Puncher Data Processing
Android - AsyncTask

The class android/os/AsyncTaskAsyncTask simplify background thread creation. The AsyncTask is not tied to the activity lifecycle. The virtual machine will hold the AsyncTask alive as long as the AsyncTask...
Card Puncher Data Processing
Android - Bundle

Key value pair structure Fragment Arguments Thee savedInstanceState Bundle is restoring information once the fragment has been running. The bundle can save information: across orientation...
Content Provider
Android - Content Provider

A content provider is a facade between the data store (Xml, Database) and the applications. Change the underlying data source without changing the application code Leverage standard android library....
Card Puncher Data Processing
Android - EmptyView

Message in the string.xml file to give a feedback In the onCreateView method See udacity/Advanced_Android_Development/blob/2.01_Empty_Database/app/src/main/java/com/example/android/sunshine/app/ForecastFragment.javaForecastFragment.java...
Android Tablet Handset Design
Android - Fragment (with or without UI)

Fragment is sort of like a “sub Activity” that you can reuse in different activities. Each fragment should be designed as a modular and reusable activity component because each fragment defines: ...
Card Puncher Data Processing
Android - Inflate (a layout XML to a view UI object)

In Android, you can create an UI: decoratively through a layout XML with Java object view (viewgroup for container and views for widget) In this context, Inflate means reading a layout XML (often...
Card Puncher Data Processing
Android - Screen Rotation

When the screen orientation changes, the system destroys and recreates the activity in order to apply alternative resources that might be available for the new screen configuration. The following lifecycle...
Android Account Sunshine
Android - Sync Manager Framework (SyncAdapter)

Introduced in Android Level 5. SyncAdapters are meant to keep local data on the device (a cache) in sync with data on the web with the goal of: fast load times, offline functionality and not...



Share this page:
Follow us:
Task Runner