0

我有一个带有主要活动和弹出活动的应用程序,如果我留在应用程序中没有问题。但是,如果弹出活动已打开并且您从最近的屏幕重新进入应用程序,则只会显示弹出活动。如果您随后关闭它,您不会返回到主要活动,但应用程序会完全关闭。

这将从主要活动中打开弹出活动:

intent = new Intent(ctx, popupActivity.class);
intent.putExtra("rackName", resultChest.get(2));

ctx.startActivity(intent);

这是关闭弹出活动的方法:

@Override
public boolean onTouchEvent(MotionEvent event) {
    finish();
    return super.onTouchEvent(event);
}

AndroidManifest.xml:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_lchr"
    android:logo="@mipmap/ic_lchr"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_lchr_round"
    android:supportsRtl="true">
    <activity android:name=".MainActivity"
        android:theme="@style/AppTheme">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
    <activity android:name=".popupActivity"
        android:theme="@style/popupTheme"
        android:launchMode = "singleInstance">
    </activity>
</application>

解决方案
正如@RedWolf 提到android:launchMode = "singleInstance"的那样,导致活动在另一个任务中打开,这导致了我的问题。
为了解决这个问题并防止弹出活动打开两次,我不得不使用android:launchMode = "singleTop"弹出活动。

4

2 回答 2

1

你可以这样做

转到 popupActivity.java 然后覆盖名为 OnBackPressed 的方法并添加 finish(); 对它

代码

@Override
public void onBackPressed() {
      finish();
  }
于 2018-08-13T08:34:52.503 回答
1

不要使用android:launchMode = "singleInstance",它会创建一个新任务。

于 2018-08-13T08:54:53.800 回答