2 Activity Life Cycle
Algorithm:
Step 1) Create a new Android Studio Project named as ActivityLifeCycle. Step 2)Goto Project Structure and open MainActivity.java file
Step 3) implement the code for all Activity Life Cycle methods.
Step 4) Add a Log message to all lifecycle methods for displaying the content on the Logcat. Log.d(tag:"lifecycle",msg:"onMethodname");
Step 5) Run the application from the menu bar select Run and Click on Run App option.
Android Activity Lifecycle methods
Method
|
Description
|
onCreate
|
called when activity is first created.
|
onStart
|
called when activity is becoming visible to the user.
|
onResume
|
called when activity will start interacting with the user.
|
onPause
|
called when activity is not visible to the user.
|
onStop
|
called when activity is no longer visible to the user.
|
onRestart
|
called after your activity is stopped, prior to the start.
|
onDestroy
|
called before the activity is destroyed.
|
MainActivity.java
package com.example.activitylifecycle;
import android.support.v7.app.AppCompatActivity; import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity { @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"); }
}
Output:
09-15 08:10:11.052 2058-2058/com.example.activitylifecycle D/lifecycle: onCreate invoked
09-15 08:10:11.052 2058-2058/com.example.activitylifecycle D/lifecycle: onStart invoked onResume invoked
09-15 08:10:19.222 2058-2058/com.example.activitylifecycle D/lifecycle: onPause invoked
09-15 08:10:19.752 2058-2058/com.example.activitylifecycle D/lifecycle: onStop invoked onDestroy invoked.
should we place anything in layout my phone screen is showing empty
ReplyDeleteThis comment has been removed by the author.
ReplyDelete