java如何在最近屏幕后返回到上一个活动
我有一个带有主活动和弹出活动的应用程序,如果我留在应用程序中,就没有问题。但是,如果弹出活动已打开,并且您从“最近”屏幕重新输入应用程序,则仅显示弹出活动。如果随后关闭,则不会返回主活动,但应用程序将完全关闭
这将从主活动打开弹出式活动:
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);
}
雄蜂座。xml:
<application
安卓:allowBackup="true"
安卓:icon="@mipmap/ic_lchr"
安卓:logo="@mipmap/ic_lchr"
安卓:label="@string/app_name"
安卓:roundIcon="@mipmap/ic_lchr_round"
安卓:supportsRtl="true">
<activity 安卓:name=".MainActivity"
安卓:theme="@style/AppTheme">
<intent-filter>
<action 安卓:name="安卓.intent.action.MAIN" />
<category 安卓:name="安卓.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity 安卓:name=".popupActivity"
安卓:theme="@style/popupTheme"
安卓:launchMode = "singleInstance">
</activity>
</application>
解决方案:
正如@RedWolf所提到的,安卓:launchMode = "singleInstance"
导致活动在另一个任务中打开,这导致了我的问题
为了解决这个问题并防止弹出活动打开两次,我必须对弹出活动使用安卓:launchMode = "singleTop"
# 1 楼答案
你可以这样做
转到弹出式活动。那么爪哇 重写名为OnBackPressed的方法 并添加finish();对它
代码
# 2 楼答案
不要使用
android:launchMode = "singleInstance"
,它将创建一个新任务