Android Activity Lifecycle
✅ Activity Lifecycle क्या है?
Android Activity Lifecycle 7 मुख्य callback methods से मिलकर बना है:
-
onCreate()
-
जब activity पहली बार बनाई जाती है, तब call होता है।
-
यहां पर आप UI set करते हैं (
setContentView()
), initialization करते हैं।
-
-
onStart()
-
Activity user को दिखाई देने के लिए तैयार होती है।
-
-
onResume()
-
Activity foreground में आ जाती है और user interaction के लिए तैयार होती है।
-
-
onPause()
-
जब activity partially hidden हो जाती है (जैसे कोई दूसरी activity overlay हो जाए), तब call होता है।
-
यहां पर आप heavy task (जैसे video/audio) pause कर सकते हैं।
-
-
onStop()
-
जब activity पूरी तरह से hidden हो जाती है, तब call होता है।
-
-
onRestart()
-
जब activity stop होने के बाद फिर से start होती है (जैसे user back press करके फिर से activity खोले)।
-
-
onDestroy()
-
जब activity completely destroy हो जाती है (memory से remove हो जाती है)।
✅ 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 करें?
-
App Run करें → देखो onCreate → onStart → onResume call होंगे।
-
Home button press करो → onPause → onStop call होंगे।
-
App फिर से resume करो → onRestart → onStart → onResume call होंगे।
-
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
Post a Comment