ANDROID Activity
ANDROID
An Android activity is a core component that provides a single, focused screen with UI components for user interaction.
Android Activity
➔ An activity serves as the main gateway or entry point for user interaction and manages everything visible on the screen.
➔ Typically, an activity is used for a specific task or screen, such as a login page or a product display screen.
➔ Activities are loosely coupled in nature; although activities work together, they are independent of each other.
➔ Each activity must be declared or registered in the AndroidManifest.xml file to be accessible to the system.
➔ Activities are launched using Intents, which are messaging objects and are used to request an action from another component in the app.
Launcher Activity
Activity is a very important component of Android applications and it acts as the execution entry point of every app. So any Android application contains one or more activities, and one of them is registered as the main activity, from which the application is launched. Again, the launcher activity starts execution from its life-cycle methods, and an activity runs different methods at different stages of its life-cycle.
Activity Life-Cycle Methods
The Android system manages the activity as a series of tasks through multiple callback methods, which transition the activity to different states.
onCreate(): This callback method runs when the system first creates the activity. This is where data initialization and UI layout setting is completed.
onStart(): When this callback method runs, the activity becomes visible to the user.
onResume(): This callback method brings the activity to the foreground and makes it interactive.
onPause(): This function is called when the user is leaving the activity and the activity is not accepting any user input and cannot run any code. It is still visible, but it is no longer in focus.
onStop(): onStop(): This callback method is called when the activity is no longer visible to the user and ensures complete hiding of the UI.
onRestart(): This is called when an activity in the Stopped state is about to be restarted.
onDestroy(): This function is called just before the activity is deleted from memory.
Figure: Activity Life-Cycle Methods
Activity and View
The activity contains logic and view layout contains user interface.
An activity is a class that contains business logic or logic for the application's data. This data needs to be displayed somewhere, and that is the layout part of the activity. A layout is an independent and separate XML file that acts as a view for an activity. So each activity class must have a corresponding layout file, which will be presented as a view and with which the user can interact. The Activity class has a method called setContentView() and this method allows you to set the view or layout file as a parameter to the Activity class.