Android Activity Lifecycle
What is the Android Lifecycle?
The Android Activity Lifecycle defines how an activity behaves in different states:
- Created
- Started
- Resumed
- Paused
- Stopped
- Destroyed
Lifecycle Methods
• onCreate(): Initialize your activity• onStart(): Activity is about to become visible• onResume(): Activity has become visible• onPause(): Partially obscured• onStop(): Fully obscured• onRestart(): Restarting after stop• onDestroy(): Cleanup before destruction
• onCreate(): Initialize your activity
• onStart(): Activity is about to become visible
• onResume(): Activity has become visible
• onPause(): Partially obscured
• onStop(): Fully obscured
• onRestart(): Restarting after stop
• onDestroy(): Cleanup before destruction
JAVA CODE
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- Log.d("lifecycle","onCreate invoked");
- }
- @Override
- protected void onStart() {
- super.onStart();
- Log.d("lifecycle","onStart invoked");
- }
- @Override
- protected void onResume() {
- super.onResume();
- Log.d("lifecycle","onResume invoked");
- }
- @Override
- protected void onPause() {
- super.onPause();
- Log.d("lifecycle","onPause invoked");
- }
- @Override
- protected void onStop() {
- super.onStop();
- Log.d("lifecycle","onStop invoked");
- }
- @Override
- protected void onRestart() {
- super.onRestart();
- Log.d("lifecycle","onRestart invoked");
- }
- @Override
- protected void onDestroy() {
- super.onDestroy();
- Log.d("lifecycle","onDestroy invoked");
- }
- }
- KOTLIN
- class MainActivity : Activity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- Log.d("lifecycle", "onCreate invoked")
- }
- override fun onStart() {
- super.onStart()
- Log.d("lifecycle", "onStart invoked")
- }
- override fun onResume() {
- super.onResume()
- Log.d("lifecycle", "onResume invoked")
- }
- override fun onPause() {
- super.onPause()
- Log.d("lifecycle", "onPause invoked")
- }
- override fun onStop() {
- super.onStop()
- Log.d("lifecycle", "onStop invoked")
- }
- override fun onRestart() {
- super.onRestart()
- Log.d("lifecycle", "onRestart invoked")
- }
- override fun onDestroy() {
- super.onDestroy()
- Log.d("lifecycle", "onDestroy invoked")
- }
- }
Comments
Post a Comment