最近屏幕后如何返回上一个活动

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


这将打开主活动的弹出活动:


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"用于弹出活动。


www说
浏览 128回答 2
2回答

拉风的咖菲猫

不要使用android:launchMode = "singleInstance",它会创建一个新任务。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java