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

AndroidMobile DevelopmentApps/Applications

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 = ""
   android:id = "@+id/parent"
   xmlns:tools = ""
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   tools:context = ".MainActivity"
   android:gravity = "center"
   android:orientation = "vertical">
      android:id = "@+id/enterNumber"
      android:layout_width = "match_parent"
      android:hint = "Enter phone number"
      android:layout_height = "wrap_content" />
      android:id = "@+id/text"
      android:textSize = "18sp"
      android:textAlignment = "center"
      android:text = "click"
      android:layout_width = "match_parent"
      android:layout_height = "wrap_content" />

Step 3 − Add the following code to src/

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

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

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

Step 4 − Add the following code to

<?xml version = "1.0" encoding = "utf-8"?>
<manifest xmlns:android = ""
   package = "com.example.andy.myapplication">
   <uses-permission android:name = "android.permission.VIBRATE" />
      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">
         android:name = ".MainActivity"
         android:noHistory = "true"
         android:screenOrientation = "portrait">
            <action android:name = "android.intent.action.MAIN" />
            <category android:name = "android.intent.category.LAUNCHER" />

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 22:30:24