Author: Nitish
Suppose that you are playing a game and by mistake you pressed back button, what must be the result? Should your application be closed? Your answer must be NO. So there might be a mechanism through which user must get a pop up message once he clicks on back button and if user again clicks on back button then the application must be closed as it ensures that the user is willing to exit the application. Well! so let us see how to do that.
I have named my android application as ExitOnDoubleClick.
File Structure
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="nitish.exitondoubleclick" > <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Now copy below code in your java file(MainActivity.java).
package nitish.exitondoubleclick; import android.os.Bundle; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; public class MainActivity extends AppCompatActivity { boolean doubleBackToExitPressedOnce = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public void onBackPressed() { if (doubleBackToExitPressedOnce) { super.onBackPressed(); return; } this.doubleBackToExitPressedOnce = true; Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show(); new Handler().postDelayed(new Runnable() { @Override public void run() { doubleBackToExitPressedOnce = false; } }, 2000); } }
Copy below code in your xml file(activity_main.xml).
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:background="#84927a" tools:context=".MainActivity"> <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
Now we are done with the coding, no need to modify other files of our project.
OUTPUT
Run the application, below screen will appear in front of you.
Once when you press back button, the following message will appear on your screen.
Now you can press back button again within given interval of time(2000 ms in this case) to exit the application.
There is one another method to display a confirmation message to the user which will be covered in the upcoming articles.
Also read our latest blogs.
Good job
LikeLike