How to prevent going back to the previous activity in Android?


There are so many situations, where we should not call back action when user click on back button.

This example demonstrates how to integrate Android Login and register form.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/actvity_main.xml.

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:id = "@+id/parent"
   xmlns:tools = "http://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>

Step 3 − Add the following code to src/MainActivity.java

package com.example.andy.myapplication;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
   int view = R.layout.activity_main;
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(view);
   }
   @Override
   public void onBackPressed() {
      // super.onBackPressed();
      Toast.makeText(MainActivity.this,"There is no back action",Toast.LENGTH_LONG).show();
      return;
   }
}

In the above, we have taken onBackPressed(), when user click on back button, it going to return empty as shown below -

@Override
public void onBackPressed() {
   // super.onBackPressed();
   Toast.makeText(MainActivity.this,"There is no back action",Toast.LENGTH_LONG).show();
   return;
}

Step 4 − Add the following code to manifest.java

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = "http://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>

In the above we have declared no History value is true means Main Activity doesn't maintain any previous activity information in activity manager.

Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click Run  icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −

In the above result, it has shown the default screen. When you click on back button, it will do nothing as shown below -

Click here to download the project code

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements