官术网_书友最值得收藏!

Lifecycle of an Android activity

Each screen on Android applications has a lifecycle. When you open any application, some sort of screen will welcome you. In the background, this launched screen must be created before it can convey its content to your eyes. Without you even realizing, the screen or activity experienced a couple of stages in its lifecycle. We'll discuss what lifecycle implies in Android programming.

During its lifetime, an Android activity will be in one of the following states: 

  • Running
  • Paused
  • Stopped
  • Killed

The lifecycle callback methods are as follows:

  • onCreate()
  • onStart()
  • onResume()
  • onPause()
  • onStop()
  • onDestroy()
  • onRestart()

The following diagram explains the lifecycle of an activity:

  1. onCreate(): Basically, this event gets fired just once when the activity is started. This is a good place to initialize any information, such as, variables' lists view components and so on. 
  2. onStart(): Once the activity enters the “started” state, the activity becomes visible and interactive. 
    In this method, the Android system maintains all the code that are related to the user interface and business logic of the application. 
  3. onResume(): This event is called whenever the user returns to the activity after leaving from the activity — such as receiving a call, pressing the home button, turning off the screen, or transitioning to another activity.
  4. onPause(): The activity calls this method as the first indication that the user is leaving your activity and this method sets all the process to be idle. 
  5. onStop()Using this callback activity is no longer visible to the user because either a new activity gets started or the existing activity gets the resumed state. The next callback that the system calls is either onRestart() to bring the activity back to interact with the user, or onDestroy() to terminate the activity.

 

 

  1. onDestroy()This method is called before the activity is destroyed. This is the final call that the activity receives. Using this method, we can unregister the hardware, such as accelerometer, microphone, and so on. We can terminate all the file writing operations in this method. 

Now, without waiting any further, let's get started on creating packages and start coding.

主站蜘蛛池模板: 鄢陵县| 巩义市| 缙云县| 浮梁县| 五常市| 兴隆县| 洛川县| 沂源县| 哈巴河县| 共和县| 舒兰市| 大理市| 邛崃市| 和林格尔县| 镇雄县| 邵武市| 年辖:市辖区| 阿坝县| 芮城县| 武城县| 察雅县| 翁牛特旗| 高安市| 阳东县| 富蕴县| 高青县| 通榆县| 泰来县| 青冈县| 新密市| 五河县| 黑水县| 乐陵市| 青岛市| 连平县| 中牟县| 牙克石市| 霍山县| 喀喇沁旗| 靖州| 方山县|