4

我想保留导航的顺序,所以我遵循了这个:

https://developer.android.com/guide/topics/ui/notifiers/notifications.html#NotificationResponse

和这个:

Android - 构建通知,TaskStackBuilder.addParentStack 不起作用

单击通知后,会打开 ResultActivity,如果用户单击返回,则会出现 MainActivity。

当我单击返回时,它会关闭应用程序并返回主屏幕,而不是打开 MainActivity。但是,当我重新打开它时,它已经从 MainActivity 开始了。这是我的代码:

显现:

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <receiver android:name=".AlarmReceiver" />

        <activity
            android:name=".ResultActivity"
            android:parentActivityName=".MainActivity">
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity"/>
        </activity>
    </application>

AlarmReceiver 中的 OnReceive

        builder.setContentTitle("sth");
        builder.setContentText("sth");

        TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);

        // Back button
        stackBuilder.addParentStack(ResultActivity.class);
        // The desired activity when clicking notifications
        stackBuilder.addNextIntent(new Intent(context, ResultActivity.class));

        // Create Pending Intent
        PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
        builder.setContentIntent(pendingIntent);

        // Finally a manager
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0, builder.build());

如何解决这个问题?谢谢!

4

0 回答 0