Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🟢 शुरुआत  ·  Lesson 07

Activity और Lifecycle

Activity क्या है?

Activity आपकी app की एक screen है — एक class जो AppCompatActivity extend करती है और XML layout दिखाती है। यह आप नहीं, Android तय करता है कि वह कब बने, दिखे, छिपे और destroy हो। यह आपको lifecycle callbacks से बताता है।

Lifecycle diagram

onCreate()layout set, views ढूँढो, listeners लगाओonStart()activity दिखने लगती हैonResume()सामने — user interact कर सकता है ✅ RUNNING← runningonPause()ऊपर दूसरी screen/dialogonStop()पूरी तरह छिपी (Home दबाया)← back: onRestart→onStartonDestroy()finish या rotate
onStop से वापस आने पर onRestart → onStart → onResume

हर callback log करें

Kotlin – MainActivity.kt
class MainActivity : AppCompatActivity() {
    private val TAG = "Lifecycle"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        Log.d(TAG, "onCreate – build the UI once")
    }
    override fun onStart()   { super.onStart();   Log.d(TAG, "onStart – visible") }
    override fun onResume()  { super.onResume();  Log.d(TAG, "onResume – user can interact") }
    override fun onPause()   { super.onPause();   Log.d(TAG, "onPause – losing focus") }
    override fun onStop()    { super.onStop();    Log.d(TAG, "onStop – not visible") }
    override fun onRestart() { super.onRestart(); Log.d(TAG, "onRestart – coming back") }
    override fun onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy – gone") }
}

App चलाएँ और Logcat (नीचे की bar) खोलें, tag:Lifecycle से filter करें:

Logcat – app खोली
D/Lifecycle: onCreate – build the UI once D/Lifecycle: onStart – visible D/Lifecycle: onResume – user can interact

जब… तो क्या होता है

Logcat – Home, फिर वापस
--- press Home --- D/Lifecycle: onPause – losing focus D/Lifecycle: onStop – not visible --- open the app again --- D/Lifecycle: onRestart – coming back D/Lifecycle: onStart – visible D/Lifecycle: onResume – user can interact
Logcat – screen rotation
--- rotate the phone --- D/Lifecycle: onPause – losing focus D/Lifecycle: onStop – not visible D/Lifecycle: onDestroy – gone D/Lifecycle: onCreate – build the UI once D/Lifecycle: onStart – visible D/Lifecycle: onResume – user can interact
User actionCallbacks
App खोलीonCreate → onStart → onResume
Home दबाया / दूसरी apponPause → onStop
वापस आयाonRestart → onStart → onResume
Back दबाया (screen छोड़ी)onPause → onStop → onDestroy
Phone rotate / dark mode बदलापूरा destroy + create
Dialog / permission popup आयासिर्फ़ onPause

Screen rotation और state save करना

Kotlin – CounterActivity.kt
class CounterActivity : AppCompatActivity() {
    private var count = 0
    private lateinit var tvCount: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_counter)
        tvCount = findViewById(R.id.tvCount)

        // restore after rotation (savedInstanceState is null on the first launch)
        count = savedInstanceState?.getInt(KEY_COUNT) ?: 0
        tvCount.text = count.toString()

        findViewById<Button>(R.id.btnPlus).setOnClickListener {
            count++
            tvCount.text = count.toString()
        }
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(KEY_COUNT, count)          // save before the activity is destroyed
    }

    companion object { private const val KEY_COUNT = "count" }
}
count = 5onSaveInstanceStateputInt("count", 5)rotatedestroy + createonCreate(saved)getInt → 5
Bundle recreation में बचा रहता है

बड़े data (database की lists) के लिए ViewModel use करें — MVVM lesson में सीखेंगे।

किस callback में क्या करें

Callbackयह करें
onCreatesetContentView, binding, adapters, click listeners, ViewModel observe
onStart / onStopदिखते समय ज़रूरी चीज़ें start/stop (location updates, animations)
onResume / onPauseCamera preview, sensors, video playback
onDestroyLifecycle-aware components से न जुड़े resources release

आम गलतियाँ

  • super.onXxx() भूलना → SuperNotCalledException crash।
  • Main thread पर onCreate में भारी काम → black screen / ANR। Coroutines use करें।
  • onDestroy हमेशा चलेगा मानना → ज़रूरी data पहले save करें।
  • Bundle में बड़ी list रखना → TransactionTooLargeException; ViewModel या database use करें।

सारांश

  • छह मुख्य callbacks: onCreate, onStart, onResume, onPause, onStop, onDestroy (+ onRestart)।
  • Rotation Activity दोबारा बनाता है; छोटी state onSaveInstanceState में save करें।
  • UI onCreate में set करें; सिर्फ़ दिखते समय वाला काम onStart/onStop में start/stop करें।
✅ Practice

HelloApp में logging code जोड़िए, फिर Home दबाइए, वापस आइए, rotate कीजिए और Back दबाइए। हर case के callbacks का order लिखकर table से मिलाइए।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Phone rotate करने पर data गायब क्यों हो जाता है?
Rotation Activity को destroy करके दोबारा बनाता है। छोटी values onSaveInstanceState में save करें, या data ViewModel में रखें जो rotation में बचा रहता है।
onPause और onStop में क्या फ़र्क है?
onPause तब चलता है जब activity focus खो देती है पर थोड़ी दिख सकती है (dialog या multi-window)। onStop तब चलता है जब वह पूरी तरह छिप जाती है।
क्या onDestroy हमेशा call होता है?
नहीं। Memory खाली करने के लिए Android process kill करे तो onDestroy नहीं भी चल सकता। ज़रूरी data पहले save करें, जैसे onStop में या user के बदलते ही।
← Android Tutorial (Kotlin)
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

अगला topicLogcat, Debugging और Emulator आपकी प्रगति: 7 / 43 (16%) · Lesson 8 / 43 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें