Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The no-UI Activity approach mentioned in the <a href="https://stackoverflow.com/a/10784293/570930">accepted answer</a> is what I decided on. I tried another option, but it did not work out. What I tried was this:</p> <ol> <li>In the <code>Service</code>, build an <code>Intent</code> stack with the <code>Intent</code> for <code>NewsReaderActivity</code> at the bottom of the stack and that for <code>ArticleActivity</code> at the top.</li> <li>Use <code>PendingIntent.getActivities()</code> and pass in the stack as created in Step1 to obtain <code>PendingIntent</code> representing the full stack.</li> <li><p>In <code>ArticleActivity</code>, we have the following code:</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></li> </ol> <p>So, you first target <code>ArticleActivity</code> - which does a sort of self-introspection to decide if it is useful in the current configuration. If not, it simply moves out of the way with <code>finish()</code>. Since the <code>NewsReaderActivity</code> is already present "before" <code>ArticleActivity</code> in the back-stack, this takes you to <code>NewsReaderActivity</code>.</p> <p>This seemed the perfect solution - except for one point which I had overlooked: <code>PendingIntent.getActivities()</code> only works with API 11 or above. There is a rough equivalent in the support library: <a href="http://developer.android.com/reference/android/support/v4/app/TaskStackBuilder.html" rel="nofollow noreferrer"><code>TaskStackBuilder</code></a>. One can go on adding Intents to the stack with <code>addNextIntent()</code>, and finally call <code>getPendingIntent()</code> to achieve something similar to <code>PendingIntent.getActivities()</code> (I assume).</p> <p>However, on pre-HC devices, this will result in only the <strong>top-most</strong> <code>Activity</code> from the stack being started in a new task (emphasis mine):</p> <blockquote> <p>On devices running Android 3.0 or newer, calls to the startActivities() method or sending the PendingIntent generated by getPendingIntent(int, int) will construct the synthetic back stack as prescribed. <em>On devices running older versions of the platform, these same calls will invoke the topmost activity in the supplied stack, ignoring the rest of the synthetic stack and allowing the back key to navigate back to the previous task</em>.</p> </blockquote> <p>So on pre-HC devices, pressing back from ArticleActivity will still take you back to the task that was running prior to ours. This is not what we want.</p> <p>I will probably share my project sometime soon. I was also concerned about starting fresh tasks even when I was doing intra-app notifications (for example, a "new news article" notification while I am reading an article). I'll hopefully post that as a separate question.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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