Wednesday, January 1, 2020

Activity

Android Activity


Activity


Activity in Android is something that appears on the device screen and provides user UI components to interact with the App. Generally, An Activity occupies device full screen (Except floating Activity and Activity group). An Activity creates a window on screen using setContentView(View). We can create our own Activity by Extending the Activity class.


Activity Life cycle


Activity stack



An Activity has a life cycle, where an Activity can be in four states. These states are Active or Running, Paused, Stopped or killed. The state of Activity is identified by its position in Activity stack. Activity Stack is a data structure that is used by the operating system to implement the flow of Activity execution. Multiple Activities can be placed on Activity Stack.





An Activity placed on the top of Activity Stack is in the “running” state. This Activity is available for the user to interact. Running activity has a complete focus on the screen.

When activity loses its focus on the screen and some partially visible activity or UI component comes in front, the Activity goes to a “paused” state. All members of Activity remain intact and keep activity alive. However, in extreme low memory conditions Activity can be discarded from Activity Stack by the Android System. Whenever some other Activity comes in foreground completely, the Activity goes go “stopped” state. In the stopped state, all members of Activity remain intact but the Window of Activity is not visible. If the Android system requires memory then Activity can be discarded from Activity Stack.

Android system can ask an Activity to finish or it may kill the Activity process if Activity is in paused or stopped.

Subclasses of Activity class have few methods that are responsible for the change in the state of an Activity. However, there are two methods onCreate(Bundle) and onPause() that are overridden most frequently. The first method onCreate(Bundle) is used to call setContentView(View) method to create the on-screen window and find findViewById(int) method to make a connection between XML UI components.

Any Activity can also be started with Context.startActivity() method. All the activities must be registered with the Androidmanifest.xml file. Activity life Cycle An Activity can migrate from one state to another