What is Activity in Android?
An Activity represents a single screen with a user interface like a form or page in web designing. Android applications can have more than one activity. In fact, more complex application usually have one activity for each UI screen implementation.
- For example:
- if you are programming a game, you might have the following activities.
- The introductory splash screen with the Continue to Play Game button OR Press Button to Play Game and an Exit Game button.
- The instructions screen with a scrolling text UI outlining the Game Rules.
- The high-score screen, with UI elements that allow the user to manage their high score entries player group screen, where users choose who will play the game with then The actual core game play screen itself allowing users to interface with the game.
- There are a number of ways to launch an activity, including the following:
- Designating a launch activity in the manifest file
- Launching an activity using the application context
- Launching a child activity from a parent activity for a result
Activity Life Cycle
When we create a new activity and launch for some actions to perform, Activity goes through the different states and calls some of the key methods between Start to Stop states.
Activity Life Cycle Diagram:
As Activities are created and destroyed they move in and out of the stack shown in above figure. As they do so, they transition through four possible states mentioned below:
- Active : When an Activity is at the top of the stack it is the visible, focused, foreground Activity that is receiving user input. Android will attempt to keep it alive at all costs, killing Activities further down the stack as needed, to ensure that it has the resources it needs. When another Activity becomes active, this one will be paused.
- Paused : In some cases your Activity will be visible but will not have focus; at this point it’s paused. This state is reached if a transparent or non-full-screen Activity is active in front of it. When paused, an Activity is treated as if it were active; however, it doesn’t receive user input events. In extreme cases Android will kill a paused Activity to recover resources for the active Activity. When an Activity becomes totally obscured, it is stopped.
- Stopped : When an Activity isn’t visible, it ‘‘stops.’’ The Activity will remain in memory, retaining all state information; however, it is now a candidate for termination when the system requires memory elsewhere. When an Activity is stopped it’s important to save data and the current UI state. Once an Activity has exited or closed, it becomes inactive.
- Inactive : After an Activity has been killed, and before it’s been launched, it’s inactive. Inactive Activities have been removed from the Activity stack and need to be restarted before they can be displayed and used.
The state of each Activity is determined by its position on the Activity stack, a last-in–first-out collection of all the currently running Activities.
- When a new Activity starts, the current foreground screen is moved to the top of the stack.
- If the user navigates back using the Back button, or the foreground Activity is closed, the next Activity on the stack moves up and becomes active.
- This process is illustrated in below Figure :
Key Callback Methods of Android Activities
|Purpose of Methods
|onCreate() is called when your Activity is getting created for the first time. It is called only once during the entire Activity Lifecycle. Also, you can use onCreate to initialize your variables. We can set the Activity Layout through setContentView function. In any Android application, whenever you create an Activity, the minimum method which you need to override is onCreate.
|onStart() gets called just before the Activity becomes visible to the user. onStart() is called from two places – after onRestart() and OnCreate(). You can use onStart() to reset Activity data, reinitialize variables etc.
onResume() gets called when your Activity comes into the foreground, and it becomes visible to the user. At this point, the Activity is on top of the Activity stack, and the user can start interacting with the Activity. onResume() is typically used to register Listeners, bind to Services etc.
|Whenever your Activity is becoming invisible or partially invisible, onPause() will be called. onPause() is called when another android activity comes on top of your Activity. In OnPause() we either release the resources, or save the application data, or stop background threads etc.
|onStop() is called when your Activity is no longer visible to the user. You can use this method to store the state of your application.
|It is similar to onCreate(), but onRestart() gets called only after onStop(). This is the method which you can use to know if your application is starting afresh or getting restarted. In onRestart(), you will get your application to save the state and reinitialize all the variables. onStart() gets called after this.
|This is the method which will be called when your Activity is getting killed. This is the final call the Activity will receive in its Lifecycle. When the user press back button on any Activity the foreground activity gets destroyed and control will return to the previous Activity.