What is Activity and Activity life cycle? - devshafique/Native-Android-Development-Blogs GitHub Wiki
An activity in Android is a single, focused task that a user can perform. Activities are used to create a user interface and handle user interactions. For example, an activity might display a list of contacts, allow the user to compose an email, or show a map of the user's current location.
Activities are managed by the Android operating system and are created and destroyed as the user navigates through an app. Each activity has a specific life cycle that defines its state and how it interacts with the user and other app components.
The activity life cycle is a set of states that an activity goes through as the user interacts with it. Understanding the activity life cycle is crucial for creating responsive, efficient, and easy-to-use apps.
The activity life cycle consists of several states, including:
-
Created: The activity is first created and the onCreate() method is called. This is where the activity sets up its user interface and initializes its data. This is the first stage of the activity life cycle.
-
Started: The activity becomes visible to the user and the onStart() method is called. This is where the activity starts interacting with the user, such as by displaying data or responding to user input. This is the second stage of the activity life cycle.
-
Resumed: The activity is in the foreground and has focus, and the onResume() method is called. This is where the activity can start updating its data or processing user input. This is the third stage of the activity life cycle.
-
Paused: Another activity comes into the foreground and covers the current activity, and the onPause() method is called. This is where the activity should save its data or stop any ongoing processing. This is the fourth stage of the activity life cycle.
-
Stopped: The activity is no longer visible to the user and the onStop() method is called. This is where the activity should release any resources it is using. This is the fifth stage of the activity life cycle.
-
Destroyed: The activity is destroyed and the onDestroy() method is called. This is where the activity should release any remaining resources and clean up any data. This is the final stage of the activity life cycle.
It is important to understand the activity life cycle so that you can create activities that are responsive, efficient, and easy to use. By handling the various states of the life cycle correctly, you can ensure that your activities are always in a stable state and that they use resources efficiently.
It's important to note that the life cycle of an activity can be affected by the system. For example, if the system is running low on memory, it may destroy an activity that is in the background to free up resources for other apps.
When an activity is in the created or started state, it is considered to be active. When it is in the paused or stopped state, it is considered to be inactive. When it is in the destroyed state, it is considered to be destroyed.
To maintain the consistency and stability of the app, it's important to handle the various states of the life cycle correctly. For example, when an activity is paused, it should save its data and stop any ongoing processing to prevent the loss of data. When an activity is resumed, it should retrieve the saved data and resume processing.
Different types of Activity:
In Android, there are several types of activities that can be used to create different types of user interfaces and handle different types of user interactions. Some of the most commonly used activity types include:
Standard Activity: A standard activity is the most basic type of activity and is used to create a user interface that consists of a single screen. A standard activity can be used to display data, capture user input, or perform other tasks.
Fullscreen Activity: A fullscreen activity is used to create a user interface that fills the entire screen. This type of activity is often used for immersive experiences, such as games or video players.
Dialog Activity: A dialog activity is used to create a user interface that appears as a dialog box. This type of activity is often used for tasks that require user input, such as settings or preferences.
Tabbed Activity: A tabbed activity is used to create a user interface that consists of multiple tabs. Each tab can display a different fragment, which is a type of reusable UI component. This type of activity is often used for navigation, such as browsing through different sections of an app.
Navigation Activity: A navigation activity is used to create a user interface that consists of a navigation drawer. The drawer can be used to navigate through different sections of the app. This type of activity is often used for navigation, such as browsing through different sections of an app.
SingleTask Activity: A singleTask activity is used to create a user interface that allow the user to navigate through a single task flow. This type of activity is often used for navigation through a specific flow of the app.
SingleTop Activity: A singleTop activity is used to create a user interface that allow the user to navigate through a top task flow. This type of activity is often used for navigation through a specific flow of the app.
There are several types of activities in Android that can be used to create different types of user interfaces and handle different types of user interactions. The most commonly used activity types include standard activity, fullscreen activity, dialog activity, tabbed activity, navigation activity, singleTask activity and singleTop activity.
XML and Activity Connection:
Android activity and XML is that the activity uses the XML layout file to define the user interface for the activity. The XML layout file contains a hierarchy of views, which represent the visual elements on the screen, such as buttons and text views. The activity uses this hierarchy of views to interact with the user interface and handle user interactions.
When an activity is created, the system inflates the XML layout file and creates a corresponding View object. The View object represents the hierarchy of views that make up the layout. The activity can then use the View object to access and manipulate the views in the layout.
The activity can also use the View object to handle user interactions. For example, it can set onClickListeners on buttons to respond to user clicks, or it can use findViewById() method to access a specific view in the layout and change its properties.
In addition, the activity can also use XML resources like strings, colors, and dimensions to define the layout and the behavior of the views. This makes it possible to separate the layout and behavior of the activity from the code, which can make the app more maintainable and easier to localize.
The connection between an Android activity and XML is that the activity uses the XML layout file to define the user interface for the activity. The XML layout file contains a hierarchy of views, which represent the visual elements on the screen. The activity inflates the XML layout and creates a corresponding View object, which it uses to access and manipulate the views in the layout, and handle the user interactions. The activity also use XML resources to define the layout and behavior of the views.
In summary, Activities are the backbone of an Android app. Activities are used to create the user interface and handle user interactions. Each activity has its own life cycle, and it is crucial to understand it in order to design apps that are responsive, efficient, and easy to use.