Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking on the Android back button twice to exit the app

Tags:

android

My app contains an initial splash screen, followed by a listview (main activity). Clicking on each row of listview opens each activity.

My requirement is that if we single press the back button from any of my inner activities (activities that are opened when we click the listview rows), it must navigate to my main listview, and then if we press once more from the listview the app must gets closed.

So, if I press the back button from my listview twice it will exit the app correctly. My main problem is that if I press the back button twice from any of my inner activities, my app is not getting closed. I need to press three times, instead of closing the app from any of my inner activities. Can anyone please help me ?

This is my code for exiting the app. I added this code in my main listview class..

private static final int TIME_INTERVAL = 3000; // # milliseconds, desired time passed between two back presses.
private long mBackPressed;

@Override
public void onBackPressed()
{
    if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) 
    { 
        super.onBackPressed(); 
        return;
    }
    else { Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show(); }

    mBackPressed = System.currentTimeMillis();
}
} 

my manifest.xml

<application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"> 

    <meta-data android:name="com.google.android.gms.version"
       android:value="@integer/google_play_services_version"/>


    <activity
        android:name="learnersseries.mathematics.complexnumbers.Firstintro"
         android:screenOrientation="portrait" 
         android:launchMode="singleTop"            
        android:label="@string/app_name" >


        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>
    </activity>


    <activity android:name="Myintegralpage"
        android:screenOrientation="portrait"
                   >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="myimagine"
        android:screenOrientation="portrait" 

       >
        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Myintroductionpage"
        android:screenOrientation="portrait" 

      >
        <intent-filter></intent-filter>
    </activity>
    <activity android:name="MainActivity"
        android:noHistory="false"
        android:screenOrientation="portrait" 
       >


        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Complexnumbers"
        android:screenOrientation="portrait"
         >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Equality"
        android:screenOrientation="portrait"
         >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Additionofcomplex"
        android:screenOrientation="portrait"
          >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Subtraction"
        android:screenOrientation="portrait" 
        >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="multiplication"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Division"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Conjugate"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Modulus"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Reciprocal"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Square"
        android:screenOrientation="portrait">


    </activity>
    <activity android:name="Representation"
        android:screenOrientation="portrait" >

        <intent-filter></intent-filter>
    </activity>
    <activity android:name="Argument"
        android:screenOrientation="portrait" >
like image 359
Thushara prasad Avatar asked Oct 27 '14 05:10

Thushara prasad


People also ask

How do I close an app from pressing the back button?

In order to check when the 'BACK' button is pressed, use onBackPressed() method from the Android library. Next, perform a check to see if the 'BACK' button is pressed again within 2 seconds and will close the app if it is so. Otherwise, don't exit.

How do you exit apps on Android?

Close one app: Swipe up from the bottom, hold, then let go. Swipe up on the app. Close all apps: Swipe up from the bottom, hold, then let go. Swipe from left to right.


2 Answers

Try this way,hope this will help you to solve your problem.

Take one flag doubleBackToExitPressedOnce which is by default (false ) in you Activity and when back button pressed first time flag value changed to (ture) and again back button pressed within 2 second exit from you app if back button again not pressed within 2 second set flag value (false).

private boolean backPressedToExitOnce;

@Override
public void onBackPressed() {
    if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            doubleBackToExitPressedOnce=false;                       
        }
    }, 2000);
} 
like image 58
Haresh Chhelana Avatar answered Sep 27 '22 20:09

Haresh Chhelana


This is a guaranteed working solution to exit app on 2 times back press

int doubleBackToExitPressed = 1;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBackPressed() {
    if (doubleBackToExitPressed == 2) {
        finishAffinity();
        System.exit(0);
    }
    else {
        doubleBackToExitPressed++;
        Toast.makeText(this, "Please press Back again to exit", Toast.LENGTH_SHORT).show();
    }

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressed=1;`enter code here`
        }
    }, 2000);
}
like image 36
Pardeep Bathla Avatar answered Sep 27 '22 22:09

Pardeep Bathla