onPause() Method Tutorial With Example In Android

onPause() Method In Android Activity Life Cycle:

  • When Activity is in background then onPause() method will execute
  • Activity is not visible to user and goes in background when onPause() method is executed

onPause() Example In Android:

Lets create onPause() program to understand the topic:

Final Output that we will create:

onPause Final Output
Step 1: Designing The UI for Main Activity

Create activity (content_main.xml we created here) and design a simple Button displaying “Next Activity”. We also created a textView telling user to click on the button. Below is the xml code of content_main.xml:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/layout"
    android:background="#000">

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:onClick="onClickButton"
        android:text="Next Activity"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Click On Next Activity To See onPause() Method Called"
        android:id="@+id/textView"
        android:textColor="#ffffff"
        android:layout_below="@+id/button"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="45dp"
        android:layout_marginStart="45dp" />

</RelativeLayout>

Step 2: Code MainActivity.java where use toast method to display onPause() method when the acitivity goes in background. Below is the code MainActivity.java:

package abhiandroid.com.onpauseexample;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    RelativeLayout layout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);
        layout = (RelativeLayout) findViewById(R.id.layout);

    }


    public void onClickButton(View v) {

        Intent i = new Intent(this, Second.class);
        startActivity(i);
    }


    @Override
    protected void onStart() {
        super.onStart();
        Toast.makeText(getApplicationContext(), "onStart called", Toast.LENGTH_LONG).show();
    }


    @Override
    protected void onResume() {

        super.onResume();
        Toast.makeText(getApplicationContext(), "onResumed called", Toast.LENGTH_LONG).show();

    }

    @Override
    protected void onPause() {

        super.onResume();
        Toast.makeText(getApplicationContext(), "onPause called", Toast.LENGTH_LONG).show();

    }

}

Important Note: You will get error because we have not yet created content_second.xml and Second.java file. So that error will be solved in next steps.

Step 3: Now create a content_second.xml where we will display the text “onPause() method called for previous activity because it goes in background”. Below is the content_second.xml code:

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Previous Activity Goes in Background so onPause() called for that activity"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

Step 4: In Second.java put the below code where we will simply call content_second.xml layout. Below is the code

package abhiandroid.com.onpauseexample;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class Second extends AppCompatActivity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_second);


    }

}

Step 5: Make sure your Manifests file has both the activity listed in it. Below is the code of AndroidManifest.xml:

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

    <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"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".Second"
            android:label="@string/title_activity_second"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

Output:

Step 1: Now run the App in Android Virtual Device (AVD). You will see the below output screen:

onPause example output screen
Step 2: Now click on the “Next Activity” button. You will notice toast message of onPause() method will display on the screen.

onPause Example method called
Step 3: Now press back button then that activity (MainActivity) is in background will come in front i.e. onstart() and onResume() method will execute of background activity.

onStart and onResume called back button onpause


Summary:

onPause() method is called when activity goes is in the background i.e. not visible to user. Our above example shows you how this happen.

DOWNLOAD THIS FREE eBook!

This free eBook will help you master the learning of Android App Development in Android Studio!

2 thoughts on “onPause() Method Tutorial With Example In Android”