onStop() Method Tutorial With Example In Android Studio

onStop() Method In Android Activity Life Cycle

  • When Activity is in background then onPause() method will execute. After a millisecond of that method next onStop() method will execute.
  • Means here also Activity is not visible to user when onStop() executed.
  • We will use onStop() method to stop Api calls etc. This onStop() method will
    clean up all your activities resources.

onStop() Explanation With Example In Android Studio:

Now we will create an example in which you see how onStop() method call in Android. In this example we will use toast message to display the message when this method is called.

Below is the final output of the example that we will create:

onStop Example final output screen

Step 1: Create a new project Onstopexample and create an Activity name Main Activity. You will notice content_main.xml will be created by default in Android Studio. Now design a simple button “Next Activity” and text asking user to click on the button.

Below is the 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 Button to see onStop() called after onPause() for this activity"
        android:id="@+id/textView"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true"
        android:textColor="#ffffff" />

</RelativeLayout>

Step 2: Now we will code MainActivity.java where we will use Intent to move on the Next Activity when Button is clicked. The Next Activity will be created in next step. Also we will override methods of Activity Life Cycle and use Toast message to display which method is called.

Below is the code of MainActivity.java

package abhiandroid.com.onstopexample;

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, NextActivity.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();
    }
    @Override
    protected void onStop() {
        super.onStop();  // Always call the superclass method first
        Toast.makeText(getApplicationContext(), "onStop called", Toast.LENGTH_LONG).show();
    }
}

Step 3: Now we will simply create a Next Activity content_next.xml. Here we will display the text onStop() method called for previous activity.

Below is the code of content_next.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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="onStop() called after onPause() for previous activity"
        android:id="@+id/textView2"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

Step 4: The NextActivity.java will simply call the layout content_next.xml.

Below is the code of NextActivity.java

package abhiandroid.com.onstopexample;

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 NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_next);

    }

}

Step 5: Now make sure your Manifest file has both the Activity listed in it. Below is the AndroidManifest.xml code:

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

    <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=".NextActivity"
            android:label="@string/title_activity_next"
            android:theme="@style/AppTheme.NoActionBar"></activity>
    </application>

</manifest>

Output:

Step 1: Now run the App in AVD, you will see the below output screen

onStop Example Output main screen

Step 2: Click on the “Next Activity” button and you will see onStop() method will be called for previous activity after onPause() because that activity goes in background.

onStop() method called after onPause()


Summary:

onStop() method is called after onPause() method when activity goes in background. This method can be used to stop Api calls etc.