Note that there are some explanatory texts on larger screens.

plurals
  1. POFragments and Notifications: Target different Activities from Notification; depending on screen configuration
    text
    copied!<h2>Question:</h2> <p>How to decide what <code>Activity</code> a <code>Notification</code> should launch if the target might depend on the configuration (screen size, orientation etc); as is often the case when one uses <code>Fragment</code>s?</p> <hr> <h2>Details:</h2> <p>Let's consider the <a href="http://developer.android.com/training/multiscreen/index.html" rel="noreferrer">NewsReader sample</a> that demonstrates how to use <code>Fragment</code>s to produce an app that plays well with multiple screen sizes and orientations. This app is structured as follows:</p> <ul> <li>A <code>HeadlinesFragment</code>.</li> <li>An <code>ArticleFragment</code>.</li> <li>A "main" activity (<code>NewsReaderActivity</code>). In dual pane mode, this activity contains both the fragments. In single-pane mode, it only contains the <code>HeadlinesFragment</code>.</li> <li>An <code>ArticleActivity</code>. This activity is only used in single pane mode; and it contains the <code>ArticleFragment</code>.</li> </ul> <p>Now, suppose I were to enhance this app to add a background <code>Service</code> 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:</p> <ol> <li>If there are multiple news updates, clicking on the notification should always take the user to the headlines list.</li> <li>If there's only one update, clicking on the notification should open up the brand new news article.</li> </ol> <p>Note that these requirements translate to different target activities depending on current configuration. In particular,</p> <ol> <li>Requirement (1) in either mode = <code>NewsReaderActivity</code>.</li> <li>Requirement (2) in dual-pane mode = <code>NewsReaderActivity</code>.</li> <li>Requirement (2) in single-pane mode = <code>ArticleActivity</code>.</li> </ol> <p>What would be an elegant way to achieve (2) and (3) above? I think one can safely rule out the possibility of the <code>Service</code> probing for the current configuration to decide what activity to target with the <code>PendingIntent</code>. </p> <p>One solution I thought of was to skip (2) and always do (3) - i.e., always launch <code>ArticleActivity</code> if there's only one news update. This snippet from ArticleActivity looked promising:</p> <pre><code>@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></pre> <p>This code ensures that if one is viewing the <code>ArticleActivity</code>, but switches to a configuration where it is no longer required (for example from portrait to landscape); then the activity simple closes. </p> <p>However, this won't work in our case since the intent will have the <code>FLAG_ACTIVITY_NEW_TASK</code> flag set; we would have created a new task, and there is no "previous" activity on the stack. So, calling <code>finish()</code> would just clear the entire stack.</p> <p>So, how does one decide what activity to launch from a notification, if the activity to launch depends on screen configuration?</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