Android Tutorial

Software Setup and Configuration

Android Studio

File Structure

Components

Core Topics

Layout

View

Button

Intent and Intent Filters

Toast

RecyclerView

Fragments

Adapters

Other UI Component

Image Loading Libraries

Date and Time

Material Design

Bars

Working with Google Maps

Chart

Animation

Database

Advance Android

Jetpack

Architecture

App Publish

App Monetization

Activity State Changes In Android with Example

In Android, an activity can be in one of three states:

  • Resumed (Running): The activity is in the foreground and visible to the user. This state is between onResume() and onPause().

  • Paused: The activity is partially obscured by another activity �� the other activity that's in the foreground is semi-transparent or doesn't cover the full screen. The paused activity does not receive user input and cannot execute any of the onResume() code, but the system retains its state. This state is between onPause() and onStop().

  • Stopped: The activity is completely hidden from the user and is in the background. It is between onStop() and onRestart() (if the activity comes back to the foreground) or onDestroy() (if the activity is finished or the system needs to reclaim memory).

Here's a simple example to understand the state changes:

  • Create a new Android project in Android Studio.

  • In MainActivity.java, add the following code:

import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "ActivityStateChange";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "onCreate: Activity is being created.");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart: Activity is about to be visible.");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume: Activity is now interacting with the user.");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause: Activity is partially obscured, not interacting with the user.");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop: Activity is fully obscured.");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy: Activity is about to be destroyed.");
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        Log.d(TAG, "onRestart: Activity is about to be restarted.");
    }
}
  • Run your app and observe the Logcat with the TAG filter set to "ActivityStateChange" to see how these methods are called.

For example:

  • Upon launching the app, you'll see:
onCreate: Activity is being created.
onStart: Activity is about to be visible.
onResume: Activity is now interacting with the user.
  • If you press the home button:
onPause: Activity is partially obscured, not interacting with the user.
onStop: Activity is fully obscured.
  • If you return to the app:
onRestart: Activity is about to be restarted.
onStart: Activity is about to be visible.
onResume: Activity is now interacting with the user.

These logs will help you understand the sequence in which the activity lifecycle methods are called and the states the activity goes through.

  1. Activity onSaveInstanceState example in Android:

    The onSaveInstanceState method is called before an activity is paused or stopped, giving you an opportunity to save its state. Here's an example:

    // MainActivity.java
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        private static final String KEY_COUNT = "count";
        private int count = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Check if there's a saved state
            if (savedInstanceState != null) {
                count = savedInstanceState.getInt(KEY_COUNT, 0);
            }
    
            // Use the count variable as needed
            // ...
        }
    
        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
    
            // Save the state of the activity
            outState.putInt(KEY_COUNT, count);
        }
    }
    
  2. Android onPause and onResume example code:

    The onPause method is called when the activity is about to lose focus, and onResume is called when the activity is about to gain focus. Here's an example:

    // MainActivity.java
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onPause() {
            super.onPause();
            // Code to execute when the activity is paused
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            // Code to execute when the activity is resumed
        }
    }
    
  3. Activity onRestoreInstanceState example:

    The onRestoreInstanceState method is called after onStart when the activity is being re-initialized from a previously saved state. Here's an example:

    // MainActivity.java
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        private static final String KEY_COUNT = "count";
        private int count = 0;
    
        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            super.onRestoreInstanceState(savedInstanceState);
    
            // Restore the state of the activity
            count = savedInstanceState.getInt(KEY_COUNT, 0);
        }
    }
    
  4. Activity onStart and onStop methods in Android:

    The onStart method is called when the activity becomes visible, and onStop is called when the activity is no longer visible. Here's an example:

    // MainActivity.java
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onStart() {
            super.onStart();
            // Code to execute when the activity starts
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            // Code to execute when the activity stops
        }
    }
    
  5. Example of handling onDestroy in Android Activity:

    The onDestroy method is called when the activity is being destroyed. It's a good place to release resources and perform cleanup:

    // MainActivity.java
    import android.app.Activity;
    import android.os.Bundle;
    
    public class MainActivity extends Activity {
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            // Code to execute when the activity is being destroyed
        }
    }