0
2.5kviews
Explain the life cycle of Android Activity. OR Describe Android Life-cycle activity.

Subject: Mobile Computing

Difficulty: Medium

Marks: 6 Marks


1 Answer
1
56views
  • In Android, there is one foreground application, which typically takes over the whole display except for the status line.
  • When the user runs an application, Android starts it and brings it to the foreground. From that application, the user might invoke another application, or another screen in the same application, and then another and another.
  • All these programs and screens are recorded on the application stack by the system‘s Activity Manager. At any time, the user can press the Back button to return to the previous screen on the stack. From the user‘s point of view, it works a lot like the history in a web browser. Pressing Back returns them to the previous page.
  • Internally, each user interface screen is represented by an Activity class. Each activity has its own life cycle. An application is one or more activities plus a Linux process to contain them.
  • In Android, an application can be ―alive‖ even if its process has been killed.
  • Put another way, the activity life cycle is not tied to the process life cycle.
  • During its lifetime, each activity of an Android program can be in one of several states, as shown in fig. below

Let us see commands used in this life cycle taking activity in different states:

- onCreate(Bundle): This is called when the activity first starts up. You can use it to perform one-time initialization such as creating the user interface.

- onStart(): This indicates the activity is about to be displayed to the user.

- onResume(): This is called when your activity can start interacting with the user. This is a good place to start animations and music.

- onPause(): This runs when the activity is about to go into the background, usually because another activity has been launched in front of it.

- onStop(): This is called when your activity is no longer visible to the user and it won‘t be needed for a while. If memory is tight, onStop() may never be called the system may simply terminate your process.

- onRestart(): If this method is called, it indicates your activity is being re displayed to the user from a stopped state.

- onDestroy(): This is called right before your activity is destroyed. If memory is tight, onDestroy() may never be called, the system may simply terminate your process.

enter image description here

- onSaveInstanceState(Bundle): Android will call this method to allow the activity to save per-instance state, such as a cursor position within a text field.

- onRestoreInstanceState(Bundle): This is called when the activity is being reinitialized from a state previously saved by the onSaveInstanceState() method.

Please log in to add an answer.