In this tutorial we will see how and when Activity main methods are called during a classic life cycle.

Only one activity will be used in this example in order to have something easy to understand.

Let’s see that.

First of all

During this video, I launched the application then I clicked on the home button to pause it.

After I clicked the application again from the list and it came back with the methods called.

I also used some Java code to display the current method name.

Code

MainActivity.java

// Badprog.com
package com.badprog.android.tutorial.badprogactivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.WindowManager;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
	TextView textView_1;
	BadprogHelper bh = new BadprogHelper();
	private int TIMER_VALUE = 500;
	final private int METHOD_NAME = 2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
		textView_1 = (TextView) findViewById(R.id.xml_textview_1);
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onStart() {
		super.onStart();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onResume() {
		super.onResume();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onPause() {
		super.onPause();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onStop() {
		super.onStop();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onRestart() {
		super.onRestart();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
		bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());
	}
} // end class MainActivity

BadprogHelper.java

// Badprog.com
package com.badprog.android.tutorial.badprogactivity;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class BadprogHelper {
	private TextView textView_1;
	private int number = 0;
	/**
	* Manages a timer each time the method is called.
	*
	* @param aca
	* @param textView_0
	* @param charSequence
	*/
	public void formatText(final AppCompatActivity aca, TextView textView_0, final CharSequence charSequence) {
		textView_1 = textView_0;
		textView_1.append(
		aca.getClass().getSimpleName() +
		" - " +
		++number +
		" - " +
		charSequence +
		"\n");
	}
} // end of BadprogHelper

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.badprog.android.tutorial.badprogactivity.MainActivity">

    <TextView
        android:id="@+id/xml_textview_1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:scrollbars="vertical"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:padding="10dp"
        android:textStyle="bold"

        />

</RelativeLayout>

Conclusion

One step at a time.

You understood the life cycle with one activity, you are now ready to work with multiple activities.

Great job, you did it.