首页 > 解决方案 > 当按下后退按钮时,我的应用程序会关闭最近的应用程序

问题描述

当我的活动正在运行时,如果用户按下 backPressed,活动将被关闭,并且在最近的应用程序列表中不可见。我希望它显示在最近的应用程序列表中。我怎样才能做到这一点?

@Override
public void onBackPressed() {
  super.onBackPressed();

  Intent i = new Intent(Intent.ACTION_MAIN);
  i.addCategory(Intent.CATEGORY_HOME);
  i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  startActivity(i);
}

显现:

    <activity
                android:name=".MyActivity"
                android:configChanges="mcc|mnc|locale|touchscreen
                                      |keyboard|keyboardHidden
                                      |navigation|orientation
                                      |screenLayout|screenSize
                                      |fontScale"
                android:excludeFromRecents="true"
                android:exported="true"
                android:launchMode="singleTask"
                android:screenOrientation="portrait" />

标签: androidandroid-activityback

解决方案


从 AndroidManifest 中删除此行

android:excludeFromRecents="true"

或设置为假

android:excludeFromRecents="false"

推荐阅读