info@androidpaper.co.in

Handling Back Button Press in Android

In Android, the back button is a common navigation feature available on most Android devices. By default, when the back button is pressed, the system navigates back to the previous screen or closes the current activity if there is no previous screen. However, in some cases, you may want to customize the behavior of the back button to perform specific actions or implement custom navigation logic. This could include tasks like displaying a confirmation dialog, navigating to a different screen, or handling complex navigation flows within your app.

To handle the back button press in Android, you can override the onBackPressed() method provided by the Activity class. This method is automatically called when the back button is pressed. By overriding this method, you can define your own behavior and actions to be performed when the back button is pressed. To handle the back button press, follow these steps: Override the onBackPressed() method in your activity class:

@Override
public void onBackPressed() {
    // Handle the back button press here
    // You can perform any desired action or navigation
    // For example, go back to the previous activity or close the current activity
}

Inside the onBackPressed() method, you can perform the desired action. Here are a few examples: Go back to the previous activity:

super.onBackPressed(); // Call the super method to handle the back button press as the default behavior

Close the current activity:

finish(); // Close the current activity

Show a confirmation dialog before taking action:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to exit?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish(); // Close the current activity
    }
});
builder.setNegativeButton("No", null); // Do nothing if "No" is clicked
builder.show();

To Customize back pressed we call showDialog();

 showDialog();

Another Written Description

A dialog will open when clicking on the back button pressed.

  public void showDialog() {
        final Dialog dialog = new Dialog(this);
        dialog.requestWindowFeature(1);
        dialog.setCancelable(false);
        dialog.setContentView(R.layout.exit);

        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

ImageView imageView = (ImageView) dialog.findViewById(R.id.no);
        ((ImageView) dialog.findViewById(R.id.yes)).setOnClickListener(new View.OnClickListener() {
            public final void onClick(View view) {
                //MainActivity.this.finish();
                dialog.dismiss();
            }
        });

        imageView.setOnClickListener(new View.OnClickListener() {
            public final void onClick(View view) {
                dialog.dismiss();
                finish();
                System.exit(0);
            }
        });
        dialog.show();
    }

exit.xml layout design will be like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:aapt="http://schemas.android.com/aapt"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="@dimen/_300sdp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/_300sdp"
        android:background="@drawable/exit_round_layout"
        android:orientation="vertical">

        <TextView
            android:id="@+id/idtext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/_5sdp"
            android:layout_marginLeft="@dimen/_5sdp"
            android:layout_marginTop="@dimen/_10sdp"
            android:layout_marginEnd="@dimen/_5sdp"
            android:layout_marginRight="@dimen/_5sdp"
            android:gravity="center"
            android:text="Do You Really Want To Quit ?"
            android:textColor="#ff000000"
            android:textSize="18sp"
            android:textStyle="bold" />

        <RelativeLayout
            android:id="@+id/linear_"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:scaleType="fitCenter"

                android:src="@drawable/border"/>

           

    </LinearLayout>

    <ImageView
        android:id="@+id/no"
        android:layout_width="@dimen/_60sdp"
        android:layout_height="@dimen/_60sdp"
        android:layout_alignParentBottom="true"
        android:layout_marginStart="@dimen/_30sdp"
        android:layout_marginLeft="@dimen/_30sdp"
        android:src="@drawable/wrong"
        />

    <ImageView
        android:id="@+id/yes"
        android:layout_width="@dimen/_60sdp"
        android:layout_height="@dimen/_60sdp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginEnd="@dimen/_30sdp"
        android:layout_marginRight="@dimen/_30sdp"
        android:src="@drawable/right" />
</RelativeLayout>

Remember to adapt the code according to your specific requirements. Additionally, if you're working with fragments, you can handle the back button press inside a fragment by overriding the onBackPressed() method of the hosting activity and handling the fragment transactions accordingly.