android java activity lifecycle.
File: activity_main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="example.nowlearn.com.activitylifecycle.MainActivity">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Hello World!"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintRight_toRightOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
Android Activity Lifecycle Example
It provides the details about the invocation of life cycle methods of activity. In this example, we are displaying the content on the logcat.
File: MainActivity.java
- package example.nowlearn.com.activitylifecycle;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.Log;
- public class MainActivity extends Activity {
- @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");
- }
- }