Activity is like a frame or window in java that represents GUI. It represents one screen of android.
In Android, an “activity” is a fundamental component of the Android application architecture. It represents a single screen with a user interface, and it is a crucial building block for creating interactive applications. Each activity is usually defined as a Java class and corresponds to a specific task or user interaction.
Key characteristics of an Android activity include:
- User Interface (UI): An activity typically presents a UI to the user, which can include various widgets like buttons, text fields, images, etc.
- Lifecycle Management: Activities have a well-defined lifecycle, which includes methods like
onCreate()
,onStart()
,onResume()
,onPause()
,onStop()
, andonDestroy()
. These methods allow developers to manage the state and behavior of the activity throughout its existence. - Intent Handling: Activities are often started and communicated with using intents. Intents are messages that allow the components of an application to request functionality from other components, including activities.
- Task and Back Stack: Activities are managed in a task, and the Android system maintains a back stack of activities. When a new activity is started, it is pushed onto the stack. The user can navigate back through the stack by pressing the back button.
Here’s a simple example of an activity in Android:
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Your initialization and UI setup code goes here}
}
In this example, MainActivity
is an activity that sets its content view to a layout defined in the activity_main.xml
file and performs any necessary initialization in the onCreate
method.