Android - API - Life cycle of 2 activities with the Intent class

One important step in Android development is to understand how to deal with many activities.

This is what we are going to see in this Activity tutorial with an example of the famous Intent class.

Stop talking, let's get coded.

First of all

In the video you can see, in orange, the MainActivity and, in green, the Activity_2.

The title also changes with the corresponding activity.

To go to the second activity, I push the Button "GO TO ACTIVITY 2".

To come back to MainActivity, when I'm at Activity_2, I simply use the Android back button of my mobile phone.

The first activity stays with data, paused during the travel to the second activity.

And don't forget to specify the new activiy in your manifest.

Code

MainActivity.java

// Badprog.com
package com.badprog.android.tutorial.badprogactivity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
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.layout_1);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    textView_1 = (TextView) findViewById(R.id.xml_textview_1);
    setTitle("BadproG.com - MainActivity");

    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, textView_1, ste[METHOD_NAME].getMethodName());

    Button button_1 = (Button) findViewById(R.id.xml_button_1);
    button_1.setText("Go to Activity 2");

    button_1.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent(MainActivity.this, Activity_2.class);
        startActivity(intent);
      }
    });
  }

  @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

 

Activity_2.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 Activity_2 extends AppCompatActivity {
  TextView _textView_2;
  BadprogHelper bh = new BadprogHelper();
  private int TIMER_VALUE = 500;
  final private int METHOD_NAME = 2;

  protected void onCreate(Bundle saveInstanceState) {
    super.onCreate(saveInstanceState);
    setContentView(R.layout.layout_2);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
    _textView_2 = (TextView) findViewById(R.id.xml_textview_2);
    this.setTitle("BadproG.com - Activity_2");

    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());

  }

  @Override
  protected void onStart() {
    super.onStart();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

  @Override
  protected void onResume() {
    super.onResume();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

  @Override
  protected void onPause() {
    super.onPause();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

  @Override
  protected void onStop() {
    super.onStop();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

  @Override
  protected void onRestart() {
    super.onRestart();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    final StackTraceElement[] ste = Thread.currentThread().getStackTrace();
    bh.formatText(this, _textView_2, ste[METHOD_NAME].getMethodName());
  }

}

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/layout_1.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >

    <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"
        android:scrollbars="vertical"
        >

        <TextView
            android:id="@+id/xml_textview_1"
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:scrollbars="vertical"
            android:textColor="@color/colorWhite"
            android:background="@drawable/background_1"
            android:padding="10dp"
            />

        <Button
            android:id="@+id/xml_button_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/xml_textview_1"
            />

    </RelativeLayout>

</ScrollView>

res/layout/layout_2.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_2"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:scrollbars="vertical"
        android:textColor="@color/colorWhite"
        android:background="@drawable/background_2"
        android:padding="10dp"
        />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.badprog.android.tutorial.badprogactivity">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity android:name=".Activity_2">
        </activity>

    </application>

</manifest>

Conclusion

No one can't stop you now with activity life cycle!

Well done, once again you did it! laugh

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.