परिचय
यह ट्यूटोरियल दिखाता है कि जब उपयोगकर्ता बैक बटन दबाता है, तो एक कन्फर्मेशन डायलॉग कैसे दिखाएं जो पूछे "क्या आप वाकई इस एक्टिविटी को बंद करना चाहते हैं?"। यह आकस्मिक ऐप क्लोज को रोकता है।
आवश्यक चरण
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project से नया प्रोजेक्ट बनाएं।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Android एक्टिविटी छोड़ने से पहले कन्फर्मेशन डायलॉग"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3: मुख्य गतिविधि (src/MainActivity.java)
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
// कस्टम डायलॉग दिखाएं बजाय डायरेक्ट फिनिश के
new AlertDialog.Builder(this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("एक्टिविटी बंद करें")
.setMessage("क्या आप वाकई इस एक्टिविटी को बंद करना चाहते हैं?")
.setPositiveButton("हाँ", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish(); // एक्टिविटी बंद करें
Toast.makeText(MainActivity.this, "एक्टिविटी बंद हो गई", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("नहीं", null) // null = कुछ न करें, डायलॉग बंद हो जाएगा
.show();
}
}
चरण 4: मैनिफेस्ट फ़ाइल (AndroidManifest.xml)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<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=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
डिवाइस कनेक्ट करें, Run क्लिक करें। बैक बटन दबाएं - कन्फर्मेशन डायलॉग दिखाई देगा। "हाँ" चुनने पर ऐप बंद होगा, "नहीं" पर डायलॉग बंद होगा।

महत्वपूर्ण बिंदु
onBackPressed()ओवरराइड करके डिफ़ॉल्ट बैक बटन व्यवहार बदलें।AlertDialog.Builder- मटेरियल डिज़ाइन डायलॉग बनाने का मानक तरीका।setPositiveButton/setNegativeButton- बटन और उनके क्लिक लिसनर सेट करते हैं।- आधुनिक Android (13+) में
OnBackPressedDispatcherऔरOnBackPressedCallbackउपयोग करने की सिफारिश की जाती है।
आधुनिक तरीका (Android 13+ / API 33+)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
@Override
public void handleOnBackPressed() {
new AlertDialog.Builder(MainActivity.this)
.setTitle("बाहर निकलें")
.setMessage("क्या आप वाकई बाहर निकलना चाहते हैं?")
.setPositiveButton("हाँ", (dialog, which) -> finish())
.setNegativeButton("नहीं", null)
.show();
}
});
}