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

[Important Links]Download PC Games, Hollywood Movies and TV shows

Now a days, it's hard to find a website  to download your favorite contents apart from torrent which is already towards dawn. However, there are still a few sources where you can download hollywood movies and TV shows, and also PC games. Links for Movies and TV shows HDpopcorns - Click here for HDPopcorns Cyro.se - Click here for Cyro.se    Links for downloading PC Games Skidrow - Click here for Skidrow Ocean of Games - Click here for Ocean of Games DZ Repack - Click here for DZ Repack Subscribe to my youtube channel to see more of my content - Tec2u

[How To ??] Hide All IP 2017.07.09.170709 Loader

Hide ALL IP Modern-looking and clean application which enables you to conceal your real IP address, so that you can surf the web anonymously Hide ALL IP is the worlds best IP hide software, hide all your applications and games IP from snoopers & hackers, allows you to surf anonymously, prevent identity theft, and guard against hacker intrusions, all just need a click. Download Hide ALL IP now! Your IP address can link your internet activities directly to you, it can easy leak you by this IP address, Hide ALL IP protects your online identity by change your IP address to our private server’s IP and routes all your internet traffic through our encrypted internet servers so that all remote servers only get a fake IP address, you are very safely. Unlike your ISP, Hide ALL IP does not track and does not record any where you go ! Hide ALL IP is a simple software tool which can be used to protect your identity when surfing the Internet, by hiding your rea...