Android 当我从通知启动活动时,MainActivity也会启动

Android 当我从通知启动活动时,MainActivity也会启动,android,notifications,Android,Notifications,在我的应用程序中,我有一个活动,当用户单击应用程序通知时启动 有时,当用户单击通知时,活动会正确显示,但有时应用程序的main活动也会启动回正确的活动,并在用户单击正确的活动时显示 我已经尝试将“android:launchMode=singleTask”添加到 AlertActionReceiverActivity,即从通知启动的Activity,也不添加它,但结果相同 解决这个问题运气好吗 <activity android:name=".MainActivity" android:l

在我的应用程序中,我有一个
活动
,当用户单击应用程序通知时启动

有时,当用户单击通知时,
活动
会正确显示,但有时应用程序的
main活动
也会启动回正确的
活动
,并在用户单击正确的
活动
时显示

我已经尝试将“android:launchMode=singleTask”添加到
AlertActionReceiverActivity
,即从通知启动的
Activit
y,也不添加它,但结果相同

解决这个问题运气好吗

<activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>  

<activity android:name=".AlertActionReceiverActivity" android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent" />

这是因为在Android上,默认情况下,返回时显示的是父活动,而不是上一个

Android doc上解释了解决方案:使用带有特殊标志的PendingEvent:

(注意API兼容性!!)


我希望这有助于

参考此,看看它是否能帮助您在此处共享通知代码。谢谢您提供的示例,但我不确定我是否能够遵循它。。。您对Intent和PendingEvent使用了相同的变量名,并且不确定我是否理解您为什么要定义TaskStackBuilder,如果在定义之后您根本没有分配给通知。无论如何谢谢你@TheMatrix你们并没有提供任何代码,所以我只是根据我自己的变量。但是,如果这在任何情况下都有帮助,您可以向上投票。在将taskAffinity=“”修饰符添加到NotificationActivity和Intent.FLAG_ACTIVITY_CLEAR_任务添加到通知标志后,似乎工作正常。谢谢
 <activity android:name=".MainActivity" android:label="@string/app_name" android:exported="true" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>  

    <activity
        android:name=".AlertActionReceiverActivity"
        android:parentActivityName=".ABCActivity"
        android:launchMode="singleTask" android:excludeFromRecents="true" android:theme="@style/AppThemeTransparent">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".ABCActivity"/>
    </activity>
    Intent resultIntent = new Intent(this, ResultActivity.class);
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
    stackBuilder.addParentStack(ResultActivity.class);
    PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
    stackBuilder.addNextIntent(resultIntent);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    Intent notifyIntent =
        new Intent(new ComponentName(this, ResultActivity.class));
    notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 
        Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent notifyPIntent =
        PendingIntent.getActivity(
        this,
        0,
        notifyPIntent,
        PendingIntent.FLAG_UPDATE_CURRENT
    );
    builder.setContentIntent(notifyPIntent);
    NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(id, builder.build());