Skip to main content

[Android Tutorial] Press back button twice to exit app

Few Apps have the feature to close the app when back button is pressed twice. This feature comes handy when there is accidental press of back button. This feature allows the user to close the app only when back button is pressed twice.

Today we will be showing you how to implement this on your own app.

We will be using a custom toast to demonstrate this features. But you may want to try this on default toast settings too.

We will be using this on the MainActivity.java file but you may want to use accordingly to any class file. Preferred is the home activity file.

Step 1 Create a variable of integer type and assign its value to 0.
Ex - int count = 0;
Step 2 Create a method onBackPressed() to  override the default back settings.

Paste the following code -

public void onBackPressed()
{
    if(count == 1)
    {
        count=0;
        finish();
    }
    else    {
        
        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast,
                (ViewGroup) findViewById(R.id.custom_toast_container));

        TextView text = (TextView) layout.findViewById(R.id.text);
        text.setText("Press Back again to Exit");

        final Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override            public void run() {
                toast.cancel();
            }
        }, 800);
        count++;
    }

    return;
}



Step 3 Create a new .xml file named custom_toast.xml inside the res/layout/ directory.
Paste the following code - 
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/custom_toast_container"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:padding="8dp"    android:background="#FFF"    >
    <ImageView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginRight="8dp"        tools:ignore="ContentDescription,RtlHardcoded" />
    <TextView android:id="@+id/text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#FF4C4C"        />
</LinearLayout>


Step 4 Now run the app it should work perfectly.
Step 5 Enjoy! profit.
Screenshots:-

Comments

Popular posts from this blog

[ Tech News ] Google Duo picture-in-picture mode

Duo's picture-in-picture mode is now available for everyone on Oreo. Oreo introduced picture-in-picture mode for phones and tablets, and Google is now flipping the switch for Duo. The feature is now available on all Oreo devices. You don't need to make any changes to enable picture-in-picture mode. As long as you're using a device running Oreo and have the latest update installed , you should be able to use the feature. With picture-in-picture mode, you get a miniature version of the picture window in one corner of the screen when you hit the home button in the middle of a call. You can move the position of the picture window to any corner of the screen, and the video orientation will change automatically based on whether the phone is in landscape or portrait mode. Available now on Android Oreo! https://t.co/lYr30syCRV — Justin Uberti (@juberti) October 5, 2017

[ Tech News ] Google preparing customizable ‘Google Bar’

In teardown of version 7.12 of the Google app last week, we spotted several mentions of a customizable “Google Bar.” Since then, we have been able to activate the feature to reveal that future versions of the Google app that may allow users to customize the search bar widget. The latest Google app beta contains several strings referring to a “Google Bar” and the ability to alter its appearance: <string name=”google_bar”>Google Bar</string> <string name=”bar_color_title”>Bar color</string> <string name=”bar_logo_title”>Bar logo</string> <string name=”bar_shading_title”>Bar shading</string> <string name=”bar_shape_title”>Bar shape</string> Once rolled out, the new widget has an overflow icon at the very right that contains two menu items: “Customize” or “Send Feedback.” Tapping the former launches an editor where the bar’s appearance can be changed: Bar logo : Users can either select the full Google logo ...

[ Tech News ] Google Assistant on Chromebooks

We’ve heard several times now through code commits and more that Assistant on Chromebooks is coming. However, nothing has really indicated exactly when this was going to happen. That is, until now. Hidden quietly within the Google Home app is a mention that some Assistant applications will work with Chromebooks. All Assistant apps list out which platforms they are compatible with through Assistant since, for example, some don’t work on phones, but do work on Google Home. Listed in the compatibility section of the Chat with your Assistant “game” right alongside phones, Google Home, and Allo is Chromebooks, clear as day. Not only is the text listed out, but a Chromebook icon shows up right along with those for Home and phones, so it seems pretty concrete that this is something Google intended to do, but just slipped out a little early. This is where the Google Home app comes in, since within the compatibility section for Chat with your Assistant is a listing for ...