How to Make an Activity Appear Only Once in Android?

01 Sep, 2020
There are many android applications that require an activity to appear only once when the user opens the app for the first time. Here, let’s learn how to implement this in our own android app.
Why might we need an activity to appear only once?
It may be used for getting user’s login information or other one-time information so that the user does not need to go through the hassles of entering their information every time they open the app.
It may be used to display some information regarding the app which the user might not wish to see every time.
It may be used to show a one-time animation adding a better UI to the app.
In this example, let’s use SharedPreferences to implement this project. Refer to this article to learn about SharedPreferences in detail: Shared Preferences in Android with Examples.
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Working with the activity that appears only once
Working with activity_main.xml file:
Let’s start by working with the activity that should appear just once. In this example, activity_main.xml will just contain an ImageView and a TextView. Here’s how the activity_main.xml file looks like:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout     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:background="#388e3c"    android:orientation="vertical"    tools:context=".MainActivity">      <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/gfglog" />      <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center_horizontal"        android:fontFamily="sans-serif-condensed"        android:text="Thanks for visiting GeeksforGeeks.\n\nI will be launched only once!"        android:textColor="#f5f5f5"        android:textSize="35sp" />  </LinearLayout>
The activity will look like the following:
activity_main.xml
Working with MainActivity.java file:
The MainActivity.java file will contain most of the logic of the app in this example. Let’s start by creating a String called prevStarted and set it to some value. Now in the onResume() method, check if we already have the value of prevStarted in the SharedPreferences. Since it will evaluate to false when the app is launched for the first time, start the MainActivity (Which we want to appear just once) and set the value of prevStarted in the SharedPreferences. Now whenever the app starts again, it will always check if we already have the value of prevStarted stored in SharedPreferences which will always evaluate to true from now on. So it will always launch the SecondaryActivity (Which we want to appear always) from now on. Here’s how our MainActivity.java file looks like:
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;import android.content.Context;import android.content.Intent;import android.content.SharedPreferences;import android.os.Bundle;    public class MainActivity extends AppCompatActivity {    String prevStarted = "yes";    @Override    protected void onResume() {        super.onResume();        SharedPreferences sharedpreferences = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);        if (!sharedpreferences.getBoolean(prevStarted, false)) {            SharedPreferences.Editor editor = sharedpreferences.edit();            editor.putBoolean(prevStarted, Boolean.TRUE);            editor.apply();        } else {            moveToSecondary();        }    }      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }      public void moveToSecondary(){        // use an intent to travel from one activity to another.        Intent intent = new Intent(this,SecondaryActivity.class);        startActivity(intent);    }}
Step 3: Creating the activity that appears always
Working with activity_secondary.xml file:
Now that we are done with almost all the logic of the app and will create the activity that we want to appear always, activity_secondary.xml. For this example, it will contain just ImageView and a TextView. Here’s how our activity_secondary.xml looks like:
activity_secondary.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout     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"    tools:context=".MainActivity"    android:background="#388e3c"    android:orientation="vertical">        <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:src="@drawable/gfg"        android:layout_gravity="center_horizontal"/>        <TextView        android:layout_width="match_parent"        android:layout_height="match_parent"        android:padding="15dp"        android:gravity="center_horizontal"        android:text="I will be launched always.\nJust as you can count on GeeksforGeeks always!"        android:textColor="#f5f5f5"        android:textSize="30sp"/>    </LinearLayout>
The activity will look like the following:
activity_secondary.xml
Working with SecondaryActivity.java file:
There is nothing to do with the SecondaryActivity.java file. This is how the SecondaryActivity.java file looks like:
SecondaryActivity.java
import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;  public class SecondaryActivity extends AppCompatActivity {      @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_secondary);    }}
Step 4: Configuring the AndroidManifest.xml
It is important to check that the first activity which opens when the app is launched is MainActivity.java (The activity which we want to appear only once). For this, open the AndroidManifest.xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once. Here’s how our AndroidManifest.xml file looks like:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="yourPackageName">      <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:roundIcon="@mipmap/ic_launcher_round"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".SecondaryActivity"></activity>        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>  </manifest>
android
Android
Java
Java
Android
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.