How to check and execute some line of code only first time when application starts in android mobile device using SharedPreferences.
SharedPreferences gives us the facility to detect our application status so we can detect whether our android application starts the first time or not and using then we can execute some line of code or open a new activity or do anything for first time. So in this tutorial we are going to create an android application which can Determine App Starts First Time OR Not Programmatically.
Contents in this project Determine App Starts First Time OR Not Programmatically.
1. Create TextView widget in activity_main.xml file .
<TextView android:id="@+id/MsgTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Msg Show Here" 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" />
2. Create SharedPreferences, SharedPreferences.Editor, TextView and Static string object in MainActivity.java file.
SharedPreferences sharedPreferences; SharedPreferences.Editor sharedEditor ; TextView ShowMsgTextView; public static final String PassString = "Check_App_Status";
3. Set SharedPreferences mode private using context and set SharedPreferences Editor to edit mode.
sharedPreferences = getPreferences(Context.MODE_PRIVATE); sharedEditor = sharedPreferences.edit();
5. Assign ID to TextView object.
ShowMsgTextView = (TextView)findViewById(R.id.MsgTextView);
6. Create CheckAppIsOpenFirstTimeOrNot() function with return type boolean .
public boolean CheckAppIsOpenFirstTimeOrNot(){ if(sharedPreferences.getBoolean(PassString,true)){ sharedEditor.putBoolean(PassString,false); sharedEditor.commit(); sharedEditor.apply(); // If App open for first time then return true. return true; }else { // If App open second time or already opened previously then return false. return false; } }
7. Set If condition and call the CheckAppIsOpenFirstTimeOrNot() function inside it.
if(CheckAppIsOpenFirstTimeOrNot()){ ShowMsgTextView.setText("App is Launching First Time"); } else { ShowMsgTextView.setText("App is Open Second Time or Already Opened Previously"); }
Source Code :
Code for MainActivity.java file.
package com.android_examples.determineappopenfirsttime_android_examplescom; 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 PassString = "Check_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 = (TextView)findViewById(R.id.MsgTextView); if(CheckAppIsOpenFirstTimeOrNot()){ ShowMsgTextView.setText("App is Launching First Time"); } else { ShowMsgTextView.setText("App is Open Second Time or Already Opened Previously"); } } public boolean CheckAppIsOpenFirstTimeOrNot(){ if(sharedPreferences.getBoolean(PassString,true)){ sharedEditor.putBoolean(PassString,false); sharedEditor.commit(); sharedEditor.apply(); // If App open for first time then return true. return true; }else { // If App open second time or already opened previously then return false. return false; } } }
Code for activity_main.xml layout file.
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.android_examples.determineappopenfirsttime_android_examplescom.MainActivity"> <TextView android:id="@+id/MsgTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Msg Show Here" 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.support.constraint.ConstraintLayout>
Screenshots: