How to "resume" activity from notification ("non-standalone" mode)?
there is many similar questions of how to resume activity from notification.
But my case is slightly different.
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).
This activity is designed to upload file to the server and handles only one intent:
<activity android:name="FileUploader"
android:label="..."
>
<intent-filter>
<action android:name="android.intent.action.SEND"/>
<data android:mimeType="*/*"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
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.
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.
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.
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. !But! 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.
So 2 cases: 1. Resume if already launched 2. Recreate if was destroyed by system.
I have found I way (maybe this is dirty hack, but it works) to implement first:
String className = FileUploader.class.getCanonicalName();
Intent notificationIntent = null;
mAM = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Iterator<ActivityManager.RunningTaskInfo> 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<ActivityManager.RecentTaskInfo> iter2 = mAM.getRecentTasks(100, 0).iterator();
while (iter2.hasNext()) {
RecentTaskInfo ti = iter2.next();
if (pid == ti.id) {
notificationIntent = ti.baseIntent;
}
}
}
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.
But what to do with 2: how to relaunch "parent" Activity (moreover, I even don't know how 开发者_Go百科to find out which activity it was) and recreate its stack to state it was before activity was destroyed?
All "parent" activities are third-party, so I can't change their code.
Any suggestions?
Well, the only way I found to implement this is:
android:excludeFromRecents="true" android:launchMode="singleTask"
This is not 100% of what I want (eg. I can't return to "parent" application) after resume, but is better than nothing.
精彩评论