Wednesday 18 October 2017

4. Activity Lifecycle in Android

4. Activity Lifecycle in Android


Quick Points:

  • Activity Lifecycle
  • Activity Override methods

Activity Lifecycle


Activities in the system are managed as an activity stack. When a new activity is started, it is placed on the top of the stack and becomes the running activity -- the previous activity always remains below it in the stack, and will not come to the foreground again until the new activity exits.
An activity has essentially four states:
  • If an activity is in the foreground of the screen (at the top of the stack), it is active or running.
  • If an activity has lost focus but is still visible (that is, a new non-full-sized or transparent activity has focus on top of your activity), it is paused. A paused activity is completely alive (it maintains all state and member information and remains attached to the window manager), but can be killed by the system in extreme low memory situations.
  • If an activity is completely obscured by another activity, it is stopped. It still retains all state and member information, however, it is no longer visible to the user so its window is hidden and it will often be killed by the system when memory is needed elsewhere.
  • If an activity is paused or stopped, the system can drop the activity from memory by either asking it to finish, or simply killing its process. When it is displayed again to the user, it must be completely restarted and restored to its previous state.
The following diagram shows the important state paths of an Activity.


Override (Callback) Methods:


public class Activity extends ApplicationContext {

// first callback and called when the activity is first created.

    protected void onCreate(Bundle savedInstanceState);
//called when activity becomes visible to user
    protected void onStart();
//called when activity restarts after stopping it
    protected void onRestart();
//called when user starts interacting with app
    protected void onResume();
//called when the current activity is being paused and the previous activity is being resumed.
    protected void onPause();
//called when the activity is no longer visible.
    protected void onStop();
//called before the activity is destroyed by the system.
    protected void onDestroy();
}

Happy learning!!!

No comments:

Post a Comment

Extract error records while inserting into db table using JDBCIO apache beam in java

 I was inserting data into postgres db using apache beam pipeline. it works perfectly with JdbcIO write of apache beam library. But, now, i ...