Activity Lifecycle in Android

 





Activity Lifecycle in Android (Kotlin)

The Activity Lifecycle in Android is the sequence of states (or stages) an Activity goes through from the time it is created until it is destroyed.

It is controlled by the Android system using lifecycle callback methods like onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy().

(Activity Lifecycle Android me ek process hai jo batata hai ki ek Activity kab create hoti hai, kab start hoti hai, kab user ke saamne aati hai, kab pause hoti hai, kab stop hoti hai aur kab destroy hoti hai.

Android system automatically yeh lifecycle methods call karta hai jaise onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), aur onDestroy().)


🔑 The 7 Main Lifecycle Methods

  1. onCreate()

    • Called once when the activity is first created.

    • Used for initial setup: inflating layouts, binding views, initializing variables, setting up data, etc.

    • Runs before the activity becomes visible.

  2. onStart()

    • Called when the activity becomes visible to the user.

    • The activity is not yet in the foreground.

  3. onResume()

    • Called when the activity comes to the foreground and the user can interact with it.

    • Best place to start animations, sensors, or listeners.

  4. onPause()

    • Called when the activity is partially hidden (e.g., a dialog appears, or the user navigates to another activity).

    • Use it to pause animations, stop camera, save temporary data.

  5. onStop()

    • Called when the activity is no longer visible to the user.

    • Release heavy resources here (e.g., unregister broadcast receivers, stop services).

  6. onRestart()

    • Called after onStop(), when the activity is being restarted instead of created new.

    • Useful for refreshing data before onStart().

  7. onDestroy()

    • Called before the activity is destroyed (e.g., user finishes activity, system kills app).

    • Use it to clean up resources and prevent memory leaks.



// Example Code (Kotlin)
package com.example.activitylifecycle

import android.os.Bundle

class MainActivity : AppCompatActivity() {

private val TAG = "ActivityLifecycle"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d(TAG, "onCreate: Activity Created")
}

override fun onStart() {
super.onStart()
Log.d(TAG, "onStart: Activity Visible")
}

override fun onResume() {
super.onResume()
Log.d(TAG, "onResume: Activity in Foreground (Interactive)")
}

override fun onPause() {
super.onPause()
Log.d(TAG, "onPause: Activity Partially Hidden")
}

override fun onStop() {
super.onStop()
Log.d(TAG, "onStop: Activity Stopped (Not Visible)")
}

override fun onRestart() {
super.onRestart()
Log.d(TAG, "onRestart: Activity Restarted")
}

override fun onDestroy() {
super.onDestroy()
Log.d(TAG, "onDestroy: Activity Destroyed")
}
}


🎯 Possible Interview Questions

Q1. What is the difference between onCreate() and onStart()?

  • onCreate() → Called once for initialization (UI setup, variable initialization).

  • onStart() → Called every time the activity becomes visible.


Q2. What happens if the user rotates the device?

  • The activity is destroyed and recreated.

  • onPause()onStop()onDestroy() → then onCreate()onStart()onResume().


Q3. Difference between onPause() and onStop()?

  • onPause() → Activity is partially visible (e.g., dialog appears).

  • onStop() → Activity is completely hidden.


Q4. When is onRestart() called?

  • After onStop(), if the activity comes back to the foreground.


Q5. What is the best place to release resources?

  • Release UI-related resources in onPause().

  • Release heavy resources (camera, receivers, services) in onStop() or onDestroy().


Q6. Can onDestroy() always be relied upon?

  • ❌ No. If the system kills the app due to low memory, onDestroy() might not be called.

  • That’s why you should save critical data in onPause() or onSaveInstanceState().


Q7. How can you handle configuration changes without restarting the activity?

  • Use android:configChanges="orientation|screenSize" in AndroidManifest.xml.

  • Or, use ViewModel to survive configuration changes.


No comments:

Post a Comment