How to "resume" activity from notification ("non-standalone" mode)?
<p>there is many similar questions of how to resume activity from notification.</p>

<p>But my case is slightly different.</p>

<p>I have an activity which should only be launched from other activity, and never from home or applications list (let me call this "standalone" mode).</p>

<p>This activity is designed to upload file to the server and handles only one intent:</p>

<pre><code>&lt;activity android:name="FileUploader"
                  android:label="..."
                  &gt;
            &lt;intent-filter&gt;
                &lt;action android:name="android.intent.action.SEND"/&gt;
                &lt;data android:mimeType="*/*"/&gt;
                &lt;category android:name="android.intent.category.DEFAULT"/&gt;
            &lt;/intent-filter&gt;
        &lt;/activity&gt;
</code></pre>

<p>So it could be launched, for example, from Gallery or Camera by means of "Share file". But not from applications list, since there is no way to pass file which should be uploaded and I don't want to add any UI for its selection: I want to keep application as simple as possible.</p>

<p>Here is small example: launch Gallery from Home screen, then select photo, tap share and select my FileUploader from the list.
Activities stack is: GalleryMain > Gal2 > ... > FileUploader
Now if I tap Home key - I return to Home screen and after tapping of Gallery again, I return directly to my FileUploader - this is exactly what I need, so this is ok for now.</p>

<p>Now keeping in mind than upload operation takes time I add service which is actually implements upload: FileUploaderService. I also want to add notification which shows current upload progress when (and only when) user leaves FileUploader activity by pressing Home key.</p>

<p>Tapping on this notification should return user to the FileUploader activity, resuming it in case if it is already launched, or starting it again if activity was finished by the system. <strong>!But!</strong> resuming or starting it in context of "parent" activity (Gallery in example above), since creating it in its own context leads to unwanted behavior: FileUploader icon becomes to show in Recent Apps list, allowing user to launch it in "standalone" mode.</p>

<p>So 2 cases:
1. Resume if already launched
2. Recreate if was destroyed by system.</p>

<p>I have found I way (maybe this is dirty hack, but it works) to implement first:</p>

<pre><code>String className = FileUploader.class.getCanonicalName();
Intent notificationIntent = null;
mAM  = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);

Iterator&lt;ActivityManager.RunningTaskInfo&gt; iter  = mAM.getRunningTasks(1000).iterator();
int pid = -1;
while (iter.hasNext()) {
    ActivityManager.RunningTaskInfo ti = iter.next();
    if (className.equals(ti.topActivity.getClassName())) {
        pid = ti.id;
        break;
    }
}

if (pid != -1) {
    Iterator&lt;ActivityManager.RecentTaskInfo&gt; iter2 = mAM.getRecentTasks(100, 0).iterator();
    while (iter2.hasNext()) {
        RecentTaskInfo ti = iter2.next();
        if (pid == ti.id) {
            notificationIntent = ti.baseIntent;
        }
    }
}
</code></pre>

<p>With this code I'm getting intent which was used to launch "parent" activity (eg. Gallery) and then set my notification with this intent. So it resumes Gallery just like from Home screen leading user directly to my activity, which is currently on top of the stack.</p>

<p>But what to do with 2: how to relaunch "parent" Activity (moreover, I even don't know how to find out which activity it was) and recreate its stack to state it was before activity was destroyed?</p>

<p>All "parent" activities are third-party, so I can't change their code.</p>

<p>Any suggestions?</p>
