Android Activity Lifecycle: A Beginner’s Guide (Hinglish)

Pranshu Gupta
6 min readJun 8, 2024

--

Hello everyone,

Welcome back to our Android development series on Medium! Today we will be discussing about a very important topic in android development Android Activity Lifecycle. It is very important to understand Activity lifecycle for app stability and performance .

What is Android Activity Lifecycle?

The activity lifecycle is a sequence of states that an Activity goes through throughout its lifetime. Whenever an Activity is created, becomes visible, interacts, goes into the background, and is destroyed, each state is handled by different lifecycle methods. These lifecycle methods give us control over how we manage our app’s behavior in response to user actions and system events.
(Activity lifecycle wo sequence of states hai jisme ek Activity throughout its lifetime jaati hai. Jab bhi ek Activity create hoti hai, visible hoti hai, interact hoti hai, background me jaati hai, aur destroy hoti hai, har ek state ko lifecycle ke different methods handle karte hain. Yeh lifecycle methods humein control dete hain ki hum apne app ki behavior ko kaise manage karein in response to user actions aur system events.)

Whenever we run an app, the Android system creates an instance of the MainActivity.kt class in memory, known as Activity Launched. This instance of MainActivity will then transition through various lifecycle stages until it is terminated or shut down. This lifecycle management is not limited to the launch activity (MainActivity); all activities and fragments in an Android app have their own lifecycles. For example, if a user switches to another app, the Android system will move the current activity of our app to the paused state in the background. When the user returns, the system will bring our app back to the foreground, returning it to the resumed state. This lifecycle handling ensures smooth transitions and optimal resource management in response to user actions and system events.
(So jab bhi hum app run karte hai android system ek instance banata hai MainActivity.kt class in the memory this is called Activity Launched. iske baad MainActivity ka instance alag alag cycle mein time spend karega jab tak wo dead nahi hota or technically jab tak wo shutdown nahi hota .Not only launch activity(MainActivity) all the activities and fragments of android have lifecycle. for example if user uses another app then android system will change the current activity of aur app to pause state in background and when user resume android system will take our app to fore ground and back to resume state. )

https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity

Whenever we create an activity we extend AppCompatActivity class. The AppCompatActivity class extends the FragmentActivity class the Fragment Activity class extends ComponentActivity class and ComponentActivity class extends the orginal Activity class which contains all the lifecycle callback functions. When we create an instance of an activity and we have to override the lifecycle function.
(so basically jab bhi activity ka instance bante hai wo extend karta hai AppCompatActivity class ko jo extend karta hai FragmentActivity class ko jo extend karta hai ComponentActivity class ko jo extend karta hai original Activity class ko jismein lifecycle callback functions hote hai. toh jab hum activity ka instance banate hai humme lifecycle function ko override karna hota hai )

Step by Step Lifecycle of an Activity

onCreate():
so whenever we launch an activity it first goes to create state . in onCreate fuction call we perform the basic application startup logics which is performed only once in entire lifecycle of an Activity. You must have oncreate function overriden in an activity. It is the only compulsory lifecycle function all other lifecycle fuctions are temporary.
(Toh jaise hi hum ek activity ko launch karte hai wo sabse pehle create state pe ata hai . in onCreate function call Hum basic application startup logics perform karte hai , yeah ek activity ki lifecycle mein sirf ek baar hota hai. onCreate function ko override karna compulsory hota hai baki lifecycle functions ko override karna compulsory nahi hai.)

overriding onCreate function is compulsory

onStart():
After the onCreate function finishes execution, the activity enters the started state, and the system calls onStart and onResume in quick succession. The onStart function prepares the activity to enter the foreground and become interactive. This is where the app initializes the code that creates the user interface, setting the stage for user interaction and ensuring the app is ready for user input.
( Jab onCreate function execution complete kar leta hai, activity started state mein enter karti hai, aur system jaldi se onStart aur onResume call karta hai. OnStart function activity ko foreground mein aane aur interactive hone ke liye prepare karta hai. Yahi wo stage hai jahan app user interface create karne ke liye code initialize karta hai, jo user interaction ke liye stage set karta hai aur ensure karta hai ki app user input ke liye ready hai.)

onResume():
During the resume state, the app enters the foreground and becomes fully interactive with the user. The activity remains in the resumed state until something occurs to change its state, such as the user starting another activity, opening another app, the device screen turning off, or screen rotation. This state is crucial as it represents the period when the user can actively engage with the app.
( Resume state ke dauran, app foreground mein aati hai aur user ke saath fully interactive hoti hai. Activity resumed state mein tab tak rehti hai jab tak kuch aisa nahi hota jo iski state ko change kare, jaise user dusri activity start kare, dusra app khole, device screen off ho jaye, ya screen rotate ho. )

onPause():
When the user starts another activity, opens another app, the device screen turns off, or the screen rotates, the activity transitions to the paused state and the onPause() function is invoked. If the activity returns to its original state, onResume() is called. To initialize something whenever our activity resumes, we should override the onResume() function and add the necessary code there. This could include tasks like reconnecting with the server, initializing the camera, or loading a media file. Whenever the activity reaches the paused state, it shifts from the foreground to the background.
(Jab user dusri activity start karta hai, dusra app kholta hai, device screen off hoti hai, ya screen rotate hoti hai, to activity paused state mein chali jaati hai aur onPause() function invoke hota hai. Agar activity wapas apni original state mein aati hai, to onResume() call hota hai. Agar humein activity resume hone par kuch initialize karna ho, to humein onResume() function ko override karke code wahan add karna chahiye. Jaise server se reconnect karna, camera initialize karna, ya media file load karna. Jab bhi activity paused state mein pahunchti hai, to woh foreground se background mein shift ho jaati hai.)

onStop():
After the onPause state, if the activity doesn't return to the onResume state, the system will transition it to the onStop state. In the onStop state, the instance remains in memory, but the interface is destroyed. If the user navigates back to the activity, the onRestart and onStart functions are invoked to recreate the user interface.
(onPause state ke baad, agar activity onResume state mein nahi lautati hai, to system use onStop state mein transition kar dega. onStop state mein, instance memory mein bana rahega, lekin interface destroy ho jayega. Agar user activity mein wapas jaata hai, to onRestart aur onStart functions ko invoke kiya jaayega taki user interface fir se banaya ja sake.)

onDestroy():
Finally, we reach the Destroy state. In this state, the onDestroy function is invoked when the system is destroying the activity. Apart from configuration changes, when the user finishes the activity by pressing the back button, when the system needs to free up memory resources, or when the activity is explicitly destroyed by calling the finish() method, if the system determines that the activity has been idle for a prolonged period, it may also decide to destroy it.
(end mein, hum Destroy state mein pahunchte hain. Yahan, jab system activity ko destroy karta hai, tab onDestroy function ko invoke kiya jaata hai. Iske alawa, configuration changes, agar user back button dabata hai ya system ko memory resources free karne ki zarurat hoti hai, ya fir agar activity ko finish() method se explicitly destroy kiya jaata hai, to bhi activity destroy ho sakti hai. Agar activity kafi time se active nahi hai, to system use bhi destroy kar sakta hai.)

Now that you have understood the Android activity lifecycle, you can make your apps more stable and increase their performance.😉

--

--

Pranshu Gupta

hello! My name is pranshu gupta. i am an android development and opensource enthusiast. Additionally, I'm passionate about machine learning.