首页 > 解决方案 > 需要在后按的android上打开家庭活动

问题描述

在正常情况下,我有一个活动堆栈是 A -> B -> C,当我收到推送通知并实施 click_action 时,它会打开正确的活动 C。

现在,需要的是当活动 C 在按下后退按钮后从推送通知打开时,它会关闭并且整个应用程序进入后台,但我需要导航到主屏幕(活动 A)

<activity
    android:name="Activity_A"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
    android:name="Activity_B"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="stateHidden|adjustResize" />
<activity
    android:name="Activity_C"
    android:exported="false"
    android:screenOrientation="sensorPortrait"
    android:theme="@style/MyAppTheme"
    android:windowSoftInputMode="adjustPan">
    <intent-filter>
        <action android:name="Activity_C" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

标签: androidandroid-activity

解决方案


发生这种情况是因为通常以前的活动保存在堆栈中,当用户按下后退按钮时,堆栈被调用,所以从我从你的问题中了解到,如果你想通过 Activity C 打开 Home Activity,只需通过覆盖将意图从 Activity C 调用到 Home Activity Activity C 中的 onbackpressed() 方法。这是一个示例。

Intent homeIntent = new Intent(this,HomeActivity);
startActivity(homeIntent);
finish();

finish() 用于移除堆叠的活动。


推荐阅读