Android Activity Lifecycle

 

Activity Lifecycle क्या है?

Android Activity Lifecycle 7 मुख्य callback methods से मिलकर बना है:

  1. onCreate()

    • जब activity पहली बार बनाई जाती है, तब call होता है।

    • यहां पर आप UI set करते हैं (setContentView()), initialization करते हैं।

  2. onStart()

    • Activity user को दिखाई देने के लिए तैयार होती है।

  3. onResume()

    • Activity foreground में आ जाती है और user interaction के लिए तैयार होती है।

  4. onPause()

    • जब activity partially hidden हो जाती है (जैसे कोई दूसरी activity overlay हो जाए), तब call होता है।

    • यहां पर आप heavy task (जैसे video/audio) pause कर सकते हैं।

  5. onStop()

    • जब activity पूरी तरह से hidden हो जाती है, तब call होता है।

  6. onRestart()

    • जब activity stop होने के बाद फिर से start होती है (जैसे user back press करके फिर से activity खोले)।

  7. onDestroy()

    • जब activity completely destroy हो जाती है (memory से remove हो जाती है)।


onCreate() → onStart() → onResume() → [Activity Running]
→ onPause() → onStop() → onDestroy()
अगर activity फिर से foreground में आती है:
onRestart() → onStart() → onResume()


Android Studio Example (Java)

नीचे एक simple example है जिसमें हम हर lifecycle method में Toast message और Log message दिखाएंगे:


package com.example.lifecycleexample;


import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "ActivityLifecycle";


    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Toast.makeText(this, "onCreate called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onCreate called");

    }


    @Override

    protected void onStart() {

        super.onStart();

        Toast.makeText(this, "onStart called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onStart called");

    }


    @Override

    protected void onResume() {

        super.onResume();

        Toast.makeText(this, "onResume called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onResume called");

    }


    @Override

    protected void onPause() {

        super.onPause();

        Toast.makeText(this, "onPause called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onPause called");

    }


    @Override

    protected void onStop() {

        super.onStop();

        Toast.makeText(this, "onStop called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onStop called");

    }


    @Override

    protected void onRestart() {

        super.onRestart();

        Toast.makeText(this, "onRestart called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onRestart called");

    }


    @Override

    protected void onDestroy() {

        super.onDestroy();

        Toast.makeText(this, "onDestroy called", Toast.LENGTH_SHORT).show();

        Log.d(TAG, "onDestroy called");

    }

}



कैसे Test करें?

  1. App Run करें → देखो onCreate → onStart → onResume call होंगे।

  2. Home button press करोonPause → onStop call होंगे।

  3. App फिर से resume करोonRestart → onStart → onResume call होंगे।

  4. Back button press करोonPause → onStop → onDestroy call होंगे।


Real Life Use Case

  • onCreate(): Layout inflate करना, variables initialize करना।

  • onStart(): UI को visible करना।

  • onResume(): Camera start करना, sensor register करना।

  • onPause(): Camera stop करना, sensor unregister करना।

  • onStop(): Network call बंद करना, data save करना।

  • onDestroy(): Memory cleanup।


Comments

Popular posts from this blog

Git And GitHub Collaborators and teams

🎯 Retrofit से API Call कैसे करें – Android Studio में (Java के साथ Step-by-Step गाइड)

How to create React JS application