单击Android后退按钮两次退出应用程序

Thu*_*sad 6 android

我以前曾问过这个问题但不幸的是我无法找到关于这个问题的更好的解决方案.现在我的应用程序包含第一个屏幕作为启动画面然后接下来是列表视图(主要活动)然后单击列表视图的每一行打开每个活动和我的要求如果我们按下任何内部活动的后退按钮(当我们点击列表视图行时开放的活动),它必须导航到我的主列表视图,然后如果我们再次从列表视图中按下该应用程序必须关闭.因此,如果我按下我的列表视图中的后退按钮两次它将正确退出应用程序.但我的主要问题是,如果我从我的任何内部活动按两次后退按钮我的应用程序没有关闭我需要按三次而不是从我的任何内部活动关闭应用程序.任何人都可以帮助我吗?

退出应用程序的代码是我在主listview类中添加了这段代码.

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();
}
} 
Run Code Online (Sandbox Code Playgroud)

我的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" >
Run Code Online (Sandbox Code Playgroud)

Har*_*ana 14

试试这种方式,希望这可以帮助您解决问题.

在你的活动中取一个标志doubleBackToExitPressedOnce默认为(false),当第一次按下后退按钮时,标志值变为(ture),如果在2秒内没有再次按下后退按钮,则在2秒内再次按下后退按钮标志值(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);
} 
Run Code Online (Sandbox Code Playgroud)


Sud*_*han 0

尝试将父活动添加到清单中的新活动中。
例如:

<activity android:name="myimagine"
          android:screenOrientation="portrait"
          android:parentActivityName="com.example.previous_activity" />
Run Code Online (Sandbox Code Playgroud)