Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is a really good question!</p> <blockquote> <p>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.</p> </blockquote> <p>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 <code>createArticlePendingIntent()</code> method on <code>NewsReaderActivity</code> that the service uses to build its <code>Notification</code>).</p> <blockquote> <p>So, how does one decide what activity to launch from a notification, if the activity to launch depends on screen configuration?</p> </blockquote> <p>Use a <code>getActivity()</code> <code>PendingIntent</code> for <code>NewsReaderActivity</code> in your <code>Notification</code>, with enough extras that <code>NewsReaderActivity</code> knows that it is in this "show the article" scenario. Before it calls <code>setContentView()</code>, have it determine if <code>ArticleActivity</code> is the right answer. If so, <code>NewsReaderActivity</code> calls <code>startActivity()</code> to launch <code>ArticleActivity</code>, then calls <code>finish()</code> to get rid of itself (or not, if you want BACK from the article to go to <code>NewsReaderActivity</code>).</p> <p>Or, use a <code>getActivity()</code> <code>PendingIntent</code> for <code>ICanHazArticleActivity</code> in your <code>Notification</code>. <code>ICanHazArticleActivity</code> has <code>Theme.NoDisplay</code>, so it will not have a UI. It makes the decision of whether to launch <code>NewsReaderActivity</code> or <code>ArticleActivity</code>, calls <code>startActivity()</code> on the right answer, and then calls <code>finish()</code>. The advantage over the previous solution is that there is no brief flash of <code>NewsReaderActivity</code> if the end destination is <code>ArticleActivity</code>.</p> <p>Or, use the <code>createArticlePendingIntent()</code> option I mentioned in the first paragraph of my answer.</p> <p>There may be other options as well, but those are what come to mind.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload