What is Android Activity Life Cycle?
Android is the open-source operating system which is useful for many tasks. When you start or open your android application. it will undergo various states and that is called as Android Activity Life Cycle.
To learn android fundamentals please visit:android training online
Introduction to Android
Android is an open-source operating system which is based on Linux with a java programmming interface for mobile devices like Smartphones (Touch Screen Devices who supports Android OS).
It comprises of a multiple API
to support location-based services such as GPS. It also
has extensive support for multimedia hardware control to perform playback or recording using camera and microphone. It supports multi-tasking, we can move from one task window to another and multiple applications can run simultaneously It will give a chance to reuse the application components and the replacement of native applications.
With this, let’s move further and know what is the Android activity life cycle.
What is Android Activity Life Cycle?
As a user navigates through the app, Activity instances in your app transition through different stages in their life-cycle. The Activity class provides a number of callbacks that allow the activity to know that a state has changed: that the system is creating, stopping, or resuming an activity, or destroying the process in which the activity resides.
Now let’s know the Android Activity Life Cycle in a more detailed manner with the help of life cycle methods and callbacks.
Life Cycle Methods and Callbacks
In general, activity lifecycle has seven callback methods:
- onCreate()
- onStart()
- onResume()
- onPause()
- onStop()
- onRestart()
- onDestroy()
Now let’s get into the details of Android Activity Life cycle methods and callbacks. Take a look at the below figure to understand the life cycle.
You must be aware that a program starts from a main() function in different programming languages. Similarly, android initiates the program within an activity with a call to onCreate() callback method. There is a sequence of callback methods that starts up an activity and then tear down in different methods shown in the above Activity life cycle diagram:
1. onCreate(): In this state, the activity is created.
2. onStart(): This callback method is called when the activity becomes visible to the user.
3. onResume(): The activity is in the foreground and the user can interact with it.
4. onPause(): Activity is partially obscured by another activity. Another activity that’s in the foreground is semi-transparent.
5. onStop(): The activity is completely hidden and not visible to the user.
6. onRestart(): From the Stopped state, the activity either comes back to interact with the user or the activity is finished running and goes away. If the activity comes back, the system invokes
onRestart()
7. onDestroy(): Activity is destroyed and removed from the memory.
So these are the various methods of the Activity Life Cycle. Now let’s see the situations where the life cycle methods and states will occur.
- When you open the app it will go through below states:
onCreate() –> onStart() –> onResume()
- When you press the back button and exit the apponPaused() — > onStop() –> onDestory()
- When you press the home button onPaused() –> onStop()
- After pressing the home button, again when you open the app from a recent task list onRestart() –> onStart() –> onResume()
- After dismissing the dialog or back button from the dialogonResume()
- If a phone is ringing and user is using the apponPause() –> onResume()
- After the call endsonResume()
- When your phone screen is offonPaused() –> onStop()
- When your phone screen is turned back ononRestart() –> onStart() –> onResume()
So these are some of the situations when your app goes through various states.
Demo: Implement Activity LifeCycle
Step1: First you need to build a simple Android App using either java or kotlin programming language. In this demo, I am using Kotlin programming language because Kotlin has a fewer number of lines of code when compared to Java. If you wish to know how to create an Android App using Java, kindly refer to Android tutorial. Also, if you want to create an Android Application using Kotlin, then check out this article on kotlin android tutorial
Step 2: Once you build your app, you need to configure your MainActivity.kt class file and override the callbacks methods. Let’s look at the below code to understand this in a broader gauge.
package
com.example.activitycycle
import
androidx.appcompat.app.AppCompatActivity
import
android.os.Bundle
import
android.util.Log
import
android.widget.Toast
class
MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
print(
"***App state: OnCreate***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnCreate"
,Toast.LENGTH_LONG).show();
}
override fun onStart() {
super
.onStart()
print(
"***App state: OnStart***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnStart"
,Toast.LENGTH_LONG).show();
}
override fun onResume() {
super
.onResume()
print(
"***App state: OnResume***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnResume"
,Toast.LENGTH_LONG).show();
}
override fun onStop() {
super
.onStop()
print(
"***App state: OnStop***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnStop"
,Toast.LENGTH_LONG).show();
}
override fun onPause() {
super
.onPause()
print(
"***App state: OnPause***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnPause"
,Toast.LENGTH_LONG).show();
}
override fun onRestart() {
super
.onRestart()
print(
"***App state: OnReStart***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnRestart"
,Toast.LENGTH_LONG).show();
}
override fun onDestroy() {
super
.onDestroy()
print(
"***App state: OnDestroy***n"
)
Toast.makeText(getApplicationContext(),
"App state: OnDestroy"
,Toast.LENGTH_LONG).show();
}
}
Basically, in the above example, I am overriding all the methods and printing the states. Also, I have used
Toast.makeText()
, to display the life cycle methods in the app itself. Whenever application goes through various states, it invokes and displays the methods in the app itself. That’s how Android activity life cycle works.
To more information visit OnlineItGuru android course tutorial Blog
Comments
Post a Comment