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

[Antivirus] Avast! Internet Security & Premier 2017 v17.5.23.03 Final Keys

Avast! 2017 The Most Trusted Security – 220 million people choose AVAST to keep them safe online – more than any security company. avast! is the most trusted name in the antivirus industry, actively protecting more than 220 million people around the world. We’ve been protecting devices and data for over 25 years, in over 40 languages, on every populated continent. What’s New? A whole new user interface of avast Fixed slow loading of UI UI now works on smaller resolutions e.g. 800×600 Browser addons cleaning now added to Smart Scan Localization is completed Much More… How to activate? Download & Install Avast Double click license file to insert it to avast Enjoy Screenshots Downloads Avast! Internet Security 2017 v17.5.23.03 Final + Keys.rar  |   Mirror Avast! Premier 2017 v17.5.23.03 Final + Keys.rar  |   Mirror

[ Phone Launch ]Motorola launches Moto C in India

Motorola Mobility today announced the launch of its next affordable smartphone in India, named Moto C. The smartphone was officially confirmed by the company few weeks before. The Moto C features the expected Motorola design with a micro-textured back cover. There has been no mention of Moto C Plus which was also announced along with the Moto C. "According to the reports fewer people are expected to upgrade to smartphones because of concerns over price, need and ability to use. We see this as a huge opportunity for a brand like us to initiate the smartphone journey for feature phone users. At Motorola, we focus on mobility through device innovation, user-centric design and smart connectivity to offer the best possible smartphone experience to the consumers. – Sudhin Mathur, Managing Director, Motorola Mobility India & Country Head, Lenovo Mobile Biz Group"   Motorola Moto C Specifications Operating System: Android 7.0 Nougat Processor: MediaTek...

[Video Converter] Wondershare Video Converter Ultimate v10.0.5.81 Patch

Wondershare Video Converter Ultimate Much more than a converter – your complete video toolbox. Convert, edit, enhance, download, burn, organize, capture, and watch – in over 150 formats. Enjoying your videos just got simpler. Now you can watch anytime, anywhere. Alter, enhance, convert – whatever you do – the final result will be in perfect quality ( even in high-definition or 3D) no missing key frames, redraw issues, or crashes. Key Features All-in-One DVD Video Converter Universal DVD and Video Converter Convert DRM protected files to common media files Total playback freedom on almost all video/audio portable devices Amazing Video to DVD Converter Professional video download and transfer Download videos from online video sharing websites Transfer media files bwteen PC and portable devices Amazing video/DVD editor Screenshots Downloads Wondershare Video Converter Ultimate v10.0.5.81 Patch.zip  |   Mirror ...