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

[ How to ?? ] Root the Samsung Galaxy Note 8 Exynos Variant

We’re starting to see more and more people getting their hands on the new Samsung Galaxy Note 8, so for those of you looking for some help in rooting your device, XDA Recognized Developer  dr.ketan  has made a simple step-by-step guide for you. This guide will walk you through how to gain root access to the device using SuperSU . The developer also promises to provide an EFS backup tool so you can save the important data in this partition. Once your device is rooted, you open up the device to some interesting modifications such as flashing the popular audio mod called Viper4Android . Of course, there are already a ton of things you can do without root on Samsung devices such as blocking ads and disabling bloatware, but having root access is an added benefit nonetheless. Check out this guide in Galaxy Note 8 forum

[How to ??] Manually Change the Galaxy S8 Navigation Bar Color

The Samsung Galaxy S8 and Galaxy S8+ are the first flagship smartphones from Samsung that ditch their traditional capacitive buttons in favor of software navigation keys. Besides allowing us to customize the layout of the keys, we can also change the navigation bar color as well. However, the company removed this color wheel option in June’s OTA update, but thanks to an ADB command we can still manually change the Galaxy S8 navigation bar color. The guild below walks you through how to change the color via ADB through a PC, but you can also do it through a terminal emulator application on the phone provided you have root access. Tutorial – Change Galaxy S8 Navigation Bar Color Grab the USB drivers for the Galaxy S8/S8+ (Google provides a list of some  universal USB drivers here ). Download the  ADB binary  for your specific operating system ( Windows ,  Mac ,  Linux ). These will always be the latest version. Extract the ZIP file into any folder on your PC.

[ TechLeaks ] Korean media: Galaxy Note 8 to launch on August 26 in New York as report says

The Korea Herald and the news portal Naver said today purporting to reveal the release date of Samsung’s Next Big Thing. According to the Korea Herald , Samsung is planning to introduce the Galaxy Note 8 “in the third or fourth week of August.” Like the Note 7 before it, the new S Pen device will reportedly make its debut in New York. Citing an unnamed Samsung official, Naver ventures a specific date: August 26, give or take a couple of days. Samsung initially planned to launch the new Note at IFA in the first week of September, according to the Korea Herald . The impending arrival of rival devices from LG and especially Apple might have prompted Samsung to bring up the big release. For reference, the Note 7 was announced on August 2 last year. Reports that surfaced after the phone was recalled due to faulty batteries suggested Samsung rushed the device to market in order to outshine Apple. Almost every flagship release from Samsung from the last few years has been preced