0

i'm working in and android application my app contains more than 10 activities(say activity A,B etc) in which the first activity is a splash screen and next one is a list view on selecting items in a listview navigating to another different activities.my problem is that when i single press hardware back button from any inner activities(A,B,C etc)it must navigate to the listview
and when i double press the back button from any other activities the app must get closed.till here the app works perfectly.but my problem is that when i press the back button once from my any of the inner page it navigate to listview then if i press the back button once from listview my app is geting exited i dont want this i need my app gets closed only when i double click the back button from the listview.is it possible?

my code for button press is this

public void onBackPressed() {
        Intent intent = new Intent(Intent.ACTION_MAIN);
        intent.addCategory(Intent.CATEGORY_HOME);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
}

and my manifest.xml

       <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"
         >
Rustam
  • 6,485
  • 1
  • 25
  • 25
Thushara prasad
  • 155
  • 1
  • 2
  • 15
  • Possible duplicate of [Android close app on back button](http://stackoverflow.com/questions/19109529/android-close-app-on-back-button) – Lokesh Tiwari Nov 19 '15 at 19:52

1 Answers1

0
@Override
public void onBackPressed() {

    if (back_pressed + 1000 > System.currentTimeMillis()) { //back_pressed is long

           //double click

    } else {
            back_pressed = System.currentTimeMillis();
            //single click
    }

}
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
  • no its not working.i'm forced to click the hardware back button thrice from any inner activities to exit the app now. – Thushara prasad Oct 25 '14 at 08:51
  • increase 1000 to 2000. It's number of milliseconds between clicks. – Seshu Vinay Oct 25 '14 at 08:52
  • also if i double press the back button from listview my app get closed.the problem is that if i press twice from any inner activities its not getting closed . – Thushara prasad Oct 25 '14 at 08:53
  • This code just detects if button is single clicked or double clicked. Let me know if this works by just placing a toast. And I thought you should be able to handle the rest. – Seshu Vinay Oct 25 '14 at 08:55
  • private static final int TIME_INTERVAL = 2000; 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(); } } – Thushara prasad Oct 25 '14 at 08:56
  • no.only in the main activity .shall i need to add it in all my activities?? – Thushara prasad Oct 25 '14 at 09:05
  • sorry.how is it possible?if i press back button once from my inner activities it must navigate to my listview .if i add this code to my inner activities i will be forced to press the back button twice to go to the listview. – Thushara prasad Oct 25 '14 at 09:13