Unit - 1 The Lifecycle of an Android Activity



Lesson Plan: Unit - 01
Subject: P15A2AAD - Android Application Development
Topic of Study: The Lifecycle of an Android Activity
Grade/Level: Master of Computer Applications
Objective: To explore the methods of Android Activity
Time Allotment: 55 Minutes
  • The Life cycle of an Android
    • Android operating system allows multiple applications to run concurrently.
    • There can be only one active application visible to the user at a time.
    • The Android operating system keeps track of all Activity objects running by placing them on an Activity stack.
    • When a new Activity starts, the Activity on the top of the stack and the new Activity pushes onto the top of the stack.
    • Exa. Activity Stack 
    • Different important state changes within the Activity life cycle.
    • Figure:

Initializing Static Activity Data in onCreate()

  • When an Activity first starts, the onCreate() method is called.
  • The onCreate() method has a single parameter, a Bundle, which is null if this is a newly started Activity. 
  • If this Activity was killed for memory reasons and is now restarted, the Bundle contains the previous state information for this Activity so that it can re-initiate. 
  • It is appropriate to perform any setup, such as layout and data binding, in the onCreate() method.
  • This includes calls to the setContentView() method.

Initializing and Retrieving Activity Data in onResume()

  • When the Activity reaches the top of the activity stack and becomes the foreground process, the onResume() method is called.
  • Although the Activity might not be visible yet to the user, this is the most appropriate place to retrieve any instances to resources that the Activity needs to run. 
  • The onResume() method is the appropriate place to start audio, video, and animations.

Stopping, Saving, and Releasing Activity Data in onPause()

  • When another Activity moves to the top of the activity stack, the current Activity is informed that it is being pushed down the activity stack by way of the onPause() method.
  • The onPause() method can also be the last chance for the Activity to clean up and release any resources it does not need while in the background.
  • The Activity needs to perform anything in the onPause() method quickly.

Avoiding Activity Objects Being Killed.

  • Under low-memory conditions, the Android operating system can kill the process for any Activity that has been paused, stopped, or destroyed.
  • This essentially means that any Activity not in the foreground is subject to a possible shutdown. 
  • If the Activity is killed after onPause(), the onStop() and onDestroy() methods might not be called.
  • The act of killing an Activity does not remove it from the activity stack. 

Saving Activity State into a Bundle with onSaveInstanceState()

  • If an Activity is vulnerable to being killed by the Android operating system due to low memory, the Activity can save state information to a Bundle object using the onSaveInstanceState() callback method.
  • When this Activity is returned to later, this Bundle is passed into the onCreate() method, allowing the Activity to return to the exact state it was in when the Activity paused.
  • You can also read Bundle information after the onStart() callback method using the onRestoreInstanceState() callback.

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form