Subscribe Us

Activity Life Cycle in Android Development.

One of the most important must know the topic is the Activity life cycle. You need to understand what is Activity and why should we use it.

Activity is basically a simple Java class that inherits AppCompatActivity.When you extend this class now your class becomes an Activity which is a basic building block of User Interface in Android Application.



Android lifecycle methods must know for a beginner


It's the right time to know the difference between the User interface and user interaction.


User Interface: When you design and build view group inside layout for user experience. 


User Interaction:  When you click, choose, pick and also drag-drop view group with your finger and how you feel it by using this view called User interaction. 

What are callback methods?  follow this link for crystal clear knowledge of callback methods.



These are the 7 most important callback methods to help for user interaction in Android development.
onCreate()

You must implement this callback, which fires when the system first creates the activity. On activity creation, the activity enters the Created state. In the method, you perform basic application startup logic that should happen only once for the entire life of the activity. Whenever you create Activity the first method which calls is onCreate() that initializes UI component inside layout using setContentView() method take layout reference to suppose to be initialized.
onStart()
When the activity enters the Started state, the system invokes this callback. The onStart() call makes the activity visible to the user, as the app prepares for the activity to enter the foreground and become interactive. For example, this method is where the app initializes the code that maintains the UI.

onResume()

Called when activity will start interacting with the user. Now being a user you can perform interactive events. Keep remember whenever Activity started it goes from three methods without interruption i.e 

                              onCreate() -> onStart() ->onResume.

onPause()

The system calls this method as the first indication that the user is leaving your activity (though it does not always mean the activity is being destroyed); it indicates that the activity is no longer in the foreground (though it may still be visible if the user is in multi-window mode).

onStop()
When your activity is no longer visible to the user, it has entered the Stopped state, and the system invokes the onStop() callback.

Difference between onPause() and onStop() is that in both cases Activity is not in the foreground but in onPause() it is running in the background whereas onStop() permanently stopped the app hence it will no longer available to see.
onRestart()
Called after your activity is stopped, prior to starting.

onDestroy()
onDestroy() is called before the activity is destroyed. The system invokes this callback either because:
  1. the activity is finishing (due to the user completely dismissing the activity or due to finish() being called on the activity), or
  2. the system is temporarily destroying the activity due to a configuration change (such as device rotation or multi-window mode)
   
Point to remember as shorthand rule
  • onCreate() callback call when Activity first instantiated but all views are not loaded hence not seen by the user.
  • onStart() callback call when Activity goes into Start states and Now the views are visible to the foreground.
  • onResume() callback call when Activity UI is now ready to do interactive events.

  • onPause() callback call when you press the home button for some times and applications to stack look like below attach video. Activity is visible but not in the foreground.
  • onRestart() callback call when Activity comes from onPause() method
  • onStop() method remove Activity from foreground and background too.
  • onDestroy() call when you close application and it will further call finish() method to not open the app again where it left off.


Thank you for reading. Still, have a query? Comment below I will reply to your query.




Post a Comment

0 Comments

Pages