Android Activities

Main aspects of Android activities:



Activity Lifecycle:

Activities go through a series of lifecycle states, including onCreate, onStart, onResume, onPause, onStop, onDestroy, and others.

Developers can override these methods to perform specific actions when an activity transitions between states.

Intent and Starting Activities:

Activities are typically started by an intent, which is an asynchronous message used to request functionality from other components.

To start a new activity, developers create an intent and call startActivity(intent).

Layout and User Interface:

The user interface of an activity is defined using XML layout files.

Activities can use the setContentView method to associate a layout with the activity.

Activity Stack and Back Stack:

Activities are managed in a stack, with the current activity at the top.

When a new activity is started, it is pushed onto the stack.

The back stack keeps track of the user's navigation history, allowing them to navigate back through the activities.

Communication Between Activities:

Activities can communicate with each other through intents.

Data can be passed between activities using extras in intents.

The startActivityForResult method allows one activity to start another and receive a result when the second activity finishes.

Explicit and Implicit Intents:

Activities can be initiated in two ways: direct call to a particular activity through its class name or through an intent which is created and all general services that will be requested to perform a particular action will now determine the activity to perform for you.

Activity Callbacks:

There is one more concept related to activities one has to remember: one can get a callback when the activity is created, started, resumed, paused, stopped, or destroyed.

These callbacks afford development the opportunities to perform certain actions in regard to certain points of the activity life-cycle.

Here's a simple example of an activity class:

                      
                        public class MainActivity extends AppCompatActivity {
                            @Override
                            protected void onCreate(Bundle savedInstanceState) {
                                super.onCreate(savedInstanceState);
                                setContentView(R.layout.activity_main);
                        
                                // Your initialization code here
                            }
                        
                            // Other lifecycle methods and custom methods can be overridden as needed
                        }