片段和通知:自通知针对不同的活动;根据屏幕配置 [英] Fragments and Notifications: Target different Activities from Notification; depending on screen configuration

查看:89
本文介绍了片段和通知:自通知针对不同的活动;根据屏幕配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何决定什么活动 A 通知应该启动,如果目标可能取决于配置(屏幕大小,取向等);这是常有的情况下,当一个人使用片段 S'

How to decide what Activity a Notification should launch if the target might depend on the configuration (screen size, orientation etc); as is often the case when one uses Fragments?

让我们考虑新闻阅读示例演示如何使用片段 s到制作一个应用程序,具有多种屏幕尺寸和方向打得很好。这个应用程序的结构如下:

Let's consider the NewsReader sample that demonstrates how to use Fragments to produce an app that plays well with multiple screen sizes and orientations. This app is structured as follows:

  • A HeadlinesFragment
  • ArticleFragment
  • A主要的活动( NewsReaderActivity )。在双窗格模式下,该活动包含两个片段。在单窗格模式下,它仅包含 HeadlinesFragment
  • ArticleActivity 。此活动仅适用于单一管理模式;它包含了 ArticleFragment
  • A HeadlinesFragment.
  • An ArticleFragment.
  • A "main" activity (NewsReaderActivity). In dual pane mode, this activity contains both the fragments. In single-pane mode, it only contains the HeadlinesFragment.
  • An ArticleActivity. This activity is only used in single pane mode; and it contains the ArticleFragment.

现在,假设我要加强这方面的应​​用程序添加背景服务监听新闻更新,并通知通过每当有新的新闻条目的状态栏通知用户。一个合理的要求,上市可能读起来像这样:

Now, suppose I were to enhance this app to add a background Service that listens for news updates and notifies the user via status bar notifications whenever there are new news items. A reasonable requirements listing might read like so:

  1. 如果有多个新闻更新,点击通知中应始终以用户的头条新闻列表。
  2. 如果有只有一个更新,点击通知应开辟全新的新闻文章。

请注意,这些要求转化为具体取决于当前的配置不同目标的活动。尤其是,

Note that these requirements translate to different target activities depending on current configuration. In particular,

  1. 要求(1)在任一模式= NewsReaderActivity
  2. 要求(2)双窗格模式= NewsReaderActivity
  3. 要求(2)单窗口模式= ArticleActivity
  1. Requirement (1) in either mode = NewsReaderActivity.
  2. Requirement (2) in dual-pane mode = NewsReaderActivity.
  3. Requirement (2) in single-pane mode = ArticleActivity.

什么是优雅的方式来实现上述(2)和(3)?我认为一个人可以安全地排除服务的可能性探测当前的配置来决定哪些活动来定位的 PendingIntent

What would be an elegant way to achieve (2) and (3) above? I think one can safely rule out the possibility of the Service probing for the current configuration to decide what activity to target with the PendingIntent.

一个解决方案我认为是跳过(2)始终做到(3) - 即总是启动 ArticleActivity 如果只有一个消息更新。这个片段从ArticleActivity看起来很有希望:

One solution I thought of was to skip (2) and always do (3) - i.e., always launch ArticleActivity if there's only one news update. This snippet from ArticleActivity looked promising:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //...
    //...
    // If we are in two-pane layout mode, this activity is no longer necessary
    if (getResources().getBoolean(R.bool.has_two_panes)) {
        finish();
        return;
    }

    //...
    //...
}

这code可确保如果一个正在查看 ArticleActivity ,但切换到它不再需要(例如,从纵向到横向)的配置;那么活动简单的关闭。

This code ensures that if one is viewing the ArticleActivity, but switches to a configuration where it is no longer required (for example from portrait to landscape); then the activity simple closes.

不过,这并不会在我们的案例中工作,因为意图将有 FLAG_ACTIVITY_NEW_TASK 标志设置;我们已经创建了一个新的任务,并且在堆栈上没有previous活动。因此,调用完成()只想清除整个堆栈。

However, this won't work in our case since the intent will have the FLAG_ACTIVITY_NEW_TASK flag set; we would have created a new task, and there is no "previous" activity on the stack. So, calling finish() would just clear the entire stack.

那么,一个人如何决定哪些活动从通知启动,如果启动活动取决于屏幕的配置?

So, how does one decide what activity to launch from a notification, if the activity to launch depends on screen configuration?

推荐答案

这是一个很好的问题!

我认为一个人可以安全地排除服务探测当前配置的可能性,以决定哪些活动来定位的PendingIntent。

I think one can safely rule out the possibility of the Service probing for the current configuration to decide what activity to target with the PendingIntent.

我会同意,这将是preferable保持UI决定在UI层,但其服务作出的决定肯定会是一个权宜之计的选择。您可以使用一个静态方法上的UI层的类,以保持决策code技术服务(外如静态 createArticlePendingIntent()的方法 NewsReaderActivity ,该服务使用来构建其通知)。

I'd agree that it would be preferable to keep UI decisions at the UI layer, but having the service make the decision would certainly be an expedient choice. You might use a static method on a UI layer class to keep the decision code technically outside of the service (e.g., a static createArticlePendingIntent() method on NewsReaderActivity that the service uses to build its Notification).

那么,一个人如何决定哪些活动从通知启动,如果启动活动取决于屏幕的配置?

So, how does one decide what activity to launch from a notification, if the activity to launch depends on screen configuration?

使用一个 getActivity() PendingIntent NewsReaderActivity 通知,有足够的额外的 NewsReaderActivity 知道,这是在这个展示篇的场景。它要求在的setContentView(),把它确定是否 ArticleActivity 是正确的答案。如果是这样, NewsReaderActivity 要求 startActivity()启动 ArticleActivity ,然后调用完成()来摆脱自身(或没有,如果你想返回从文章去 NewsReaderActivity )。

Use a getActivity() PendingIntent for NewsReaderActivity in your Notification, with enough extras that NewsReaderActivity knows that it is in this "show the article" scenario. Before it calls setContentView(), have it determine if ArticleActivity is the right answer. If so, NewsReaderActivity calls startActivity() to launch ArticleActivity, then calls finish() to get rid of itself (or not, if you want BACK from the article to go to NewsReaderActivity).

或者,使用 getActivity() PendingIntent ICanHazArticleActivity 通知 ICanHazArticleActivity Theme.NoDisplay ,所以它不会有一个用户界面。这使得是否推出 NewsReaderActivity ArticleActivity ,要求 startActivity()的正确答案,然后调用完成()。在previous解决方案的好处是,有一种 NewsReaderActivity 无短暂闪烁如果最终目的地是 ArticleActivity

Or, use a getActivity() PendingIntent for ICanHazArticleActivity in your Notification. ICanHazArticleActivity has Theme.NoDisplay, so it will not have a UI. It makes the decision of whether to launch NewsReaderActivity or ArticleActivity, calls startActivity() on the right answer, and then calls finish(). The advantage over the previous solution is that there is no brief flash of NewsReaderActivity if the end destination is ArticleActivity.

或者,使用 createArticlePendingIntent()选项我在答复的第一段提及。

Or, use the createArticlePendingIntent() option I mentioned in the first paragraph of my answer.

可能还有其他的选项,但是这些都是什么浮现在脑海中。

There may be other options as well, but those are what come to mind.

这篇关于片段和通知:自通知针对不同的活动;根据屏幕配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆