Android Activity Lifecycle is controlled by 7 methods of android.app.Activity class. The android Activity is the subclass of ContextThemeWrapper class.
An activity is the single screen in android. It is like window or frame of Java.
By the help of activity, you can place all your UI components or widgets in a single screen.
The 7 lifecycle method of Activity describes how activity will behave at different states.
Android Activity Lifecycle methods
Let's see the 7 lifecycle methods of android activity.
| Method | Description |
|---|---|
| onCreate | called when activity is first created. |
| onStart | called when activity is becoming visible to the user. |
| onResume | called when activity will start interacting with the user. |
| onPause | called when activity is not visible to the user. |
| onStop | called when activity is no longer visible to the user. |
| onRestart | called after your activity is stopped, prior to start. |
| onDestroy | called before the activity is destroyed. |
Android Activity Lifecycle Example
It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
File: MainActivity.java
Output:
You will not see any output on the emulator or device. You need to open logcat.
Now see on the logcat: onCreate, onStart and onResume methods are invoked.
Now click on the HOME Button. You will see onPause method is invoked.
After a while, you will see onStop method is invoked.
Now see on the emulator. It is on the home. Now click on the center button to launch the app again.
Now click on the lifecycleactivity icon.
Now see on the logcat: onRestart, onStart and onResume methods are invoked.
If you see the emulator, application is started again.
Now click on the back button. Now you will see onPause methods is invoked.
After a while, you will see onStop and onDestroy methods are invoked.
The onCreate() and onDestroy() methods are called only once throughout the activity lifecycle.
Android Intent Tutorial
Android Intent is the message that is passed between components such as activities, content providers, broadcast receivers, services etc.
It is generally used with startActivity() method to invoke activity, broadcast receivers etc.
The dictionary meaning of intent is intention or purpose. So, it can be described as the intention to do action.
The LabeledIntent is the subclass of android.content.Intent class.
Android intents are mainly used to:
- Start the service
- Launch an activity
- Display a web page
- Display a list of contacts
- Broadcast a message
- Dial a phone call etc.
Types of Android Intents
There are two types of intents in android: implicit and explicit.
1) Implicit Intent
Implicit Intent doesn't specifiy the component. In such case, intent provides information of available components provided by the system that is to be invoked.
For example, you may write the following code to view the webpage.
2) Explicit Intent
Explicit Intent specifies the component. In such case, intent provides the external class to be invoked.
To get the full code of explicit intent, visit the next page.
Android Implicit Intent Example
Let's see the simple example of implicit intent that displays a web page.
activity_main.xml
File: activity_main.xml
Activity class
File: MainActivity.java
Output:
Android Explicit Intent Example
Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity in android by explicit intent.
We can also pass the information from one activity to another using explicit intent.
Here, we are going to see an example to call one activity from another and vice-versa.
Android calling one activity from another activity example
Let's see the simple example of android explicit example that calls one activity from another and vice versa.
activity_main.xml
File: activity_main.xml
activitytwo_main.xml
File: activitytwo_main.xml
ActivityOne class
File: MainActivityOne.java
ActivityTwo class
File: MainActivityTwo.java
No comments:
Post a Comment