Activity Lifecycle
Understanding activity states and lifecycle methods
Introduction to Activity Lifecycle
Activity Lifecycle Android app development का एक महत्वपूर्ण concept है। यह एक activity के different states और उन states के बीच transition को define करता है।
Activity Lifecycle is a crucial concept in Android app development. It defines the different states of an activity and the transitions between these states.
Key Points (मुख्य बिंदु):
- Activity का lifecycle system events और user actions से control होता है
- Activity lifecycle is controlled by system events and user actions
- Activity के states को manage करने के लिए lifecycle methods का use किया जाता है
- Lifecycle methods are used to manage activity states
- Lifecycle methods को override करके custom behavior implement किया जा सकता है
- Custom behavior can be implemented by overriding lifecycle methods
Activity States (एक्टिविटी स्टेट्स)
Activity के मुख्य states निम्नलिखित हैं:
The main states of an activity are:
Lifecycle Methods (लाइफसाइकल मेथड्स)
Activity lifecycle के मुख्य methods और उनका purpose:
Main methods of activity lifecycle and their purpose:
Method (मेथड) | Description (विवरण) |
---|---|
onCreate() | Activity create होने पर call होता है। UI initialization और data setup के लिए use किया जाता है। |
onCreate() | Called when activity is created. Used for UI initialization and data setup. |
onStart() | Activity visible होने से पहले call होता है। UI preparation के लिए use किया जाता है। |
onStart() | Called before activity becomes visible. Used for UI preparation. |
onResume() | Activity user interaction के लिए ready होने पर call होता है। |
onResume() | Called when activity is ready for user interaction. |
onPause() | Activity partially hidden होने पर call होता है। Important data save करने के लिए use किया जाता है। |
onPause() | Called when activity is partially hidden. Used to save important data. |
onStop() | Activity completely hidden होने पर call होता है। Resources release करने के लिए use किया जाता है। |
onStop() | Called when activity is completely hidden. Used to release resources. |
onDestroy() | Activity destroy होने से पहले call होता है। Final cleanup के लिए use किया जाता है। |
onDestroy() | Called before activity is destroyed. Used for final cleanup. |
Implementation Example (इम्प्लीमेंटेशन उदाहरण)
Activity lifecycle methods को implement करने का example:
Example of implementing activity lifecycle methods:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// UI initialization and data setup
}
override fun onStart() {
super.onStart()
// Prepare UI for visibility
}
override fun onResume() {
super.onResume()
// Start animations or sensors
}
override fun onPause() {
// Save important data
super.onPause()
}
override fun onStop() {
// Release resources
super.onStop()
}
override fun onDestroy() {
// Final cleanup
super.onDestroy()
}
}
Best Practices (सर्वोत्तम प्रथाएं)
Activity lifecycle को handle करने के लिए best practices:
Best practices for handling activity lifecycle: