How to detect if app is open first time

How to detect if app is open first time

I am working for one of my client app and in this app we need to display a pop up message if app is running for first time to display special offer or informations. So how we did it ? Before starting the actual code please check below demo video.

As shown in the above vode we are going to use Shared Preference to check app first time run status.

Use shared preference: In this method we are going to use shared preference. In shared preference we are storing a boolean value "IsFirstRun" . When app run's first time IsFirstRun is not available in Shared preference. Below is the  MainActivity.java file code.

package com.vlemonn.blog.isappopenfirsttime;

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 {

    TextView tvMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvMessage = (TextView) findViewById(R.id.tvMessage);

        checkAppRunCount();
    }

    private void checkAppRunCount()
    {
        SharedPreferences sharedPreferences
            = getBaseContext().getSharedPreferences
            (
                "MyAppPreferenc"
                , Context.MODE_PRIVATE
            );
        boolean IsFirstRun = sharedPreferences.getBoolean("IsFirstRun",true);
        if(IsFirstRun)
        {
            tvMessage.setText("Hello vLemonn ! App is running first time");
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("IsFirstRun",false);
            editor.commit();
        }
        else
        {
            tvMessage.setText("Hello vLemonn ! App is not running first time");
        }
    }
}

 

0 Comments
Leave A Comment

Please login to post your valuable comments.

Join the newsletter

Get the latest vLemonn news first

share