Android : How to Determine an App Starts for the first time!

How to Determine an App Starts for the first time!

How to Determine an App Starts for the first time!

How to check and execute some code/Activity only for the first time when the application starts on an android mobile device.

In this case, we are going to use SharedPreferences.

Many times, In a requirement where we need to have a sign-up activity/ Guidance activity/tutorial activity, which needs to be run only the first time the application is launched,  the usual MainActivity of the app needs to be run after all other startups.

You can do experiments with it and implement different activities using intent and smart code.Also can use many activities with sliding or swapable tabs in interactive way’s.

In these days almost all application like WhatsApp, facebook like top companies using this method, when user newly installed app and need to register or sign up or any kind of app use guidance.

The idea is to save a boolean variable in the sharedPreferences of the activity, with initial value as true, and the first time the app is run, pass the intent to the signup activity to be run, and set the value of the boolean variable to false.

Creating New Project

  •  Create a new project in Android Studio from File ⇒ New Project. Select the Empty Activity and proceed. Continue to click on next button until Finish button is active, then click on Finish Button.

Getting Started with Project:-

      1. Create TextView widget in the activity_main.xml file.

 <TextView
            android:id="@+id/MsgTxtView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Msg"
            android:textSize="22dp"
            android:textColor="#000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" 
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" 
            android:textAlignment="center"/>

    2.Implement SharedPreferencesSharedPreferences.EditorTextView and Static string object in MainActivity.java file.

SharedPreferences sharedPreferences;

SharedPreferences.Editor sharedEditor ;

TextView MsgTxtView;

public static final String PassString = "App_Status";

    3. Add SharedPreferences mode private using context and set SharedPreferences Editor to edit mode.

sharedPreferences = getPreferences(Context.MODE_PRIVATE);
sharedEditor = sharedPreferences.edit();

    4. Assign an ID to TextView object.

ShowMsgTextView = (TextView)findViewById(R.id.MsgTextView);

    5. Create CheckAppIsOpenFirstTimeOrNot() function with return type boolean .

 public boolean CheckAppIsOpenFirstTimeOrNot(){

        if(sharedPreferences.getBoolean(String,true)){

            sharedEditor.putBoolean(String,false);
            sharedEditor.commit();
            sharedEditor.apply();

            // If App is Launching First Time then return true.
            return true;
        }else {

            // If  App is Already Opened Previously then return false.
            return false;
        }
    }

6. Set If condition and call the CheckAppIsOpenFirstTimeOrNot() function inside it.

 if(CheckAppIsOpenFirstTimeOrNot()){

           MsgTextView.setText("This App is Launching First Time");

        }
        else {

           MsgTextView.setText("This App is Already Opened Previously");
        }

    }

Complete Source Code:-

  •  activity_main.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"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="codernub.sample.MainActivity">

    <TextView
            android:id="@+id/MsgTxtView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Msg"
            android:textSize="22dp"
            android:textColor="#000"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" 
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" 
            android:textAlignment="center"/>
</RelativeLayout>
  •  MainActivity.java.
package codernub.sample;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    SharedPreferences sharedPreferences;
    SharedPreferences.Editor sharedEditor ;

    TextView ShowMsgTextView;

    public static final String String = "App_Status";

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

        sharedPreferences = getPreferences(Context.MODE_PRIVATE);
        sharedEditor = sharedPreferences.edit();

        ShowMsgTextView =findViewById(R.id.MsgTxtView);

        if(CheckAppIsOpenFirstTimeOrNot()){

            MsgTextView.setText("This App is Launching First Time");

        }
        else {

            MsgTextView.setText("This App is Already Opened Previously");
        }

    }

    public boolean CheckAppIsOpenFirstTimeOrNot(){

        if(sharedPreferences.getBoolean(String,true)){

            sharedEditor.putBoolean(String,false);
            sharedEditor.commit();
            sharedEditor.apply();

            // If App is Launching First Time then return true.
            return true;
        }else {

            // If  App is Already Opened Previously then return false.
            return false;
        }
    }

}

That’s It.

Founder and Owner of Swifterblog. I am a Mechanical Engineer and Tech enthusiast, Android hobbyist, and many more. I love to share my knowledge through blogs & Apps.

2 thoughts on “Android : How to Determine an App Starts for the first time!

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top