In android development, an activity plays an important role to make an application. We can understand the activities with help of following statements:
- An activity is a screen in the android application that provides the user interface for the application.
- An activity provides the user interface to the application.
- An activity is similar to a GUI form or java swing frame.
- An activity can present the several user interface components such as label, button, spinner (combo-box), list etc.
- An activity provides the user interface by which user can perform action to do something such as user wants to select the subject from the spinner, then this work can be easily done.
- Each activity has its own window in which user interface can be drawn.
- An application can have one or more than one activity and these activities can be linked to each other. For example, if we want to call another activity by clicking the button.
- An application can have only one main activity which is displayed when application will launch first time.
- When application launch then the user interface is displayed on the screen which is called the main activity in the application.
- Using one activity, we can call another activity in order to perform some actions.
- When a new activity starts then the previous activity will be stopped, but system is responsible to preserve the activity in the stack.
- When we want to see the previous activity then we have to click on back button then the previous activity is shown into the screen. It is like the Last in First Out concept (LIFO).
- When any activity is stopped because of a new activity starts then it is notified because its state is changed through the callback methods of activity life cycle.
- There are various callback methods are available which an activity may receive when activity’s state is changed.
- When the system is creating activity, stopping activity, resuming activity or destroying activity, then each callback methods method provides the functionality to perform specific work for appropriate to that state change.
- Activities are able to work independently from each other.
- In android, activity is an important part of the life cycle of an application.
- Each activity implements the Activity base class.
- In most cases, activities use the full screen mode, but we can use activities as the activity group.
To create an activity, we have to create the subclass of Activity class or an existing class in it. In the subclass, we have to implement the callback methods when the activity transitions between the various states of the life cycle such as creation of activity, stopping it, resuming it or destroying it. There are main two callback methods are used as follows:
onCreate(): We must implement this callback method. When any activity is created then system calls this callback method. Whenever we are implementing then we must initialize the essential components of the activity. Most importantly, this is where we must call setContentView() to define the layout for the user interface of activity.
onPause(): whenever we want to leave an activity then this callback method is called by the system. If any changes are made by the user should be committed at this point; if we want to save that change.