【Interface&navigation】从通知开始活动(39)


从通知启动活动时,必须保留用户的预期导航体验。点击后退应该让用户通过应用程序的正常工作流程回到主屏幕,打开“ 最近”屏幕应该将活动显示为单独的任务。要保留此导航体验,您应该在新任务中启动活动。

尽管在“ 创建通知”中描述了为通知设置抽头行为的基本方法,但此页面介绍了如何设置PendingIntent通知的操作,以便创建新 任务和后台堆栈。但究竟如何执行此操作取决于您要启动的活动类型:

经常活动
这是作为应用程序正常用户界面流程的一部分而存在的活动。因此,当用户从通知到达活动时,新任务应包括完整的后备堆栈,允许他们按Back并向上导航应用层次结构。
特别活动
用户仅在通知启动时才会看到此活动。从某种意义上说,此活动通过提供难以在通知本身中显示的信息来扩展通知UI。所以这个活动不需要后台堆栈。

设置常规活动PendingIntent


要从通知中启动“常规活动”,请设置 PendingIntent使用,TaskStackBuilder以便按如下方式创建新的后备堆栈。

定义应用的活动层次结构

通过将android:parentActivityName属性添加到<activity> 应用清单文件中的每个 元素,为活动定义自然层次结构。例如:

<activity
    android:name=".MainActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>
    <!-- MainActivity is the parent for ResultActivity -->
<activity
    android:name=".ResultActivity"
    android:parentActivityName=".MainActivity" />
    ...
</activity>

使用后台堆栈构建PendingIntent

要启动包含后台堆栈活动的活动,您需要创建一个实例TaskStackBuilder 并调用addNextIntentWithParentStack(),并将其传递 Intent给您要启动的活动。

只要您已按上述方法为每个活动定义了父活动,您就可以调用 getPendingIntent()以接收PendingIntent包含整个后台堆栈的活动。

// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, ResultActivity.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

如有必要,可以Intent通过调用向堆栈中的对象添加参数TaskStackBuilder.editIntentAt()。这有时是必要的,以确保后端堆栈中的活动在用户导航到它时显示有意义的数据。

然后你可以PendingIntent像往常一样传递给通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());

设置一个特殊活动PendingIntent


因为从通知启动的“特殊活动”不需要后台堆栈,您可以PendingIntent通过调用创建getActivity(),但您还应该确保已在清单中定义了相应的任务选项。

在清单中,将以下属性添加到 <activity> 元素中。
android:taskAffinity=""
结合FLAG_ACTIVITY_NEW_TASK您将在代码中使用的 标志,将此属性设置为空白可确保此活动不会进入应用程序的默认任务。具有应用程序默认关联的任何现有任务都不会受到影响。
android:excludeFromRecents="true"
从“ 最近”中排除新任务,以便用户不会意外导航回到它。
例如:

<activity
    android:name=".ResultActivity"
    android:launchMode="singleTask"
    android:taskAffinity=""
    android:excludeFromRecents="true">
</activity>

构建并发布通知:
创建一个Intent启动 Activity。
Activity通过setFlags()使用标志 FLAG_ACTIVITY_NEW_TASK 和 调用来 设置从一个新的空任务开始 FLAG_ACTIVITY_CLEAR_TASK。
PendingIntent 通过调用 创建一个getActivity()。
例如

Intent notifyIntent = new Intent(this, ResultActivity.class);
// Set the Activity to start in a new, empty task
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the PendingIntent
PendingIntent notifyPendingIntent = PendingIntent.getActivity(
        this, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT
);

然后你可以PendingIntent像往常一样传递给通知:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(notifyPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());

有关各种任务选项以及后端堆栈如何工作的更多信息,请阅读“ 任务”和“后台堆栈”。

联系我

QQ:94297366
微信打赏:https://pan.baidu.com/s/1dSBXk3eFZu3mAMkw3xu9KQ

公众号推荐:

【Interface&navigation】从通知开始活动(39)

猜你喜欢

转载自blog.51cto.com/4789781/2155103