एंड्रॉइड में पहली बार ऐप लॉन्च का पता लगाना
यह ट्यूटोरियल SharedPreferences का उपयोग करके यह जांचने का तरीका दिखाता है कि ऐप पहली बार लॉन्च हो रहा है या नहीं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:app="https://schemas.android.com/apk/res-auto"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tvFirstTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="लोड हो रहा है..."
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
चरण 3: MainActivity.java
package com.example.firstlaunchdemo;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private SharedPreferences sharedPreferences;
private SharedPreferences.Editor editor;
private TextView tvFirstTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvFirstTime = findViewById(R.id.tvFirstTime);
sharedPreferences = getPreferences(Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
if (isFirstTimeLaunch()) {
tvFirstTime.setText("पहली बार लॉन्च! 🎉");
} else {
tvFirstTime.setText("पहली बार नहीं (वापसी उपयोगकर्ता)");
}
}
private boolean isFirstTimeLaunch() {
// डिफ़ॉल्ट रूप से true (पहली बार मानकर)
boolean isFirst = sharedPreferences.getBoolean("is_first_launch", true);
if (isFirst) {
// भविष्य के लॉन्च के लिए false सेट करें
editor.putBoolean("is_first_launch", false);
editor.apply(); // apply() असिंक्रोनस है, commit() सिंक्रोनस
return true;
}
return false;
}
}
कैसे काम करता है
- SharedPreferences कुंजी-मान जोड़े को स्थायी रूप से संग्रहीत करता है
is_first_launchकुंजी पहली बार लॉन्च को ट्रैक करती है (डिफ़ॉल्ट: true)- पहले लॉन्च पर, हम इसे
falseपर सेट करते हैं - बाद के लॉन्च पर, यह
falseलौटाता है
ऐप चलाएं
पहला लॉन्च: "पहली बार लॉन्च! 🎉" दिखाता है
बाद के लॉन्च: "पहली बार नहीं (वापसी उपयोगकर्ता)" दिखाता है
उपयोग के मामले: ऑनबोर्डिंग स्क्रीन दिखाना, डिफ़ॉल्ट सेटिंग्स कॉन्फ़िगर करना, या पहली बार उपयोगकर्ताओं के लिए ट्यूटोरियल चलाना।