ऐसी कई स्थितियां होती हैं जहां हमें बैक बटन दबाने पर वापस जाने की कार्रवाई नहीं करनी चाहिए।
बैक बटन एक्शन को अक्षम करने का उदाहरण
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और आवश्यक विवरण भरें।
चरण 2 - लेआउट XML में कोड जोड़ें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/parent"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<EditText
android:id="@+id/enterNumber"
android:layout_width="match_parent"
android:hint="Enter phone number"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text"
android:textSize="18sp"
android:textAlignment="center"
android:text="Click"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3 - MainActivity.java में कोड जोड़ें
src/MainActivity.java में निम्नलिखित कोड जोड़ें:
package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
// super.onBackPressed(); // इसे कमेंट करके बैक एक्शन रोकें
Toast.makeText(MainActivity.this, "There is no back action", Toast.LENGTH_LONG).show();
}
}
उपरोक्त कोड में, हमने onBackPressed() को ओवरराइड किया है। जब उपयोगकर्ता बैक बटन दबाता है, तो यह खाली रिटर्न करता है:
@Override
public void onBackPressed() {
// super.onBackPressed();
Toast.makeText(MainActivity.this, "There is no back action", Toast.LENGTH_LONG).show();
}
चरण 4 - Manifest में कोड जोड़ें
AndroidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.andy.myapplication">
<uses-permission android:name="android.permission.VIBRATE" />
<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"
android:noHistory="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
उपरोक्त में हमने android:noHistory="true" घोषित किया है, जिसका अर्थ है कि Main Activity एक्टिविटी मैनेजर में किसी भी पिछली एक्टिविटी की जानकारी नहीं रखती।
ऐप चलाएं
अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें और ऐप चलाएं। बैक बटन दबाने पर कुछ नहीं होगा, केवल टोस्ट संदेश दिखेगा।