I’m writing Media files share application.
Now it is divided into 2 parts:
<application>
<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>
<service android:name=".FileUploaderService" />
</application>
FileUploader is an activity which should be launched from other activities (eg. Gallery or Camera).
FileUploaderService is a service, which actually uploads file to HTTP server. It executes several HTTP requests.
Now it is possible that user press Home button during uploading and returns to Home screen.
In this case I want to have a notification with current upload stage details which will return user to Activity when tapped.
I’m creatung it like this:
int icon = R.drawable.icon;
CharSequence tickerText = "Uploading File";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Uploading File";
CharSequence contentText = "Upload in progress. Stage #" + i;
Intent notificationIntent = new Intent(this, FileUploader.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
mNM.notify(R.layout.main, notification);
The problem is that when I press home button during upload – FileUploader activity instance which was launched from Gallery becomes “stopped” and when I tap notification – 2nd FileUploader activity instance is launched.
So is it any way to “return” to the first activity when tapping notification instead of launch new one?
I’ve tried following ways but non of them work here:
-
call finish() in FileUploader.onStop() method – activity is closed when switching from portrait to landscape.
-
set android:launchMode=”singleTask” for FileUploader activity in AndroidManifest.xml – after this my applcation starts to appear in Recent Applications list, which is not acceptable since it allows to launch FileUploader activity from there and as I mentioned above it only should be launched by sending android.intent.action.SEND intent from Gallery/Camera.
-
Use same intent as was used to launch 1st instance – 2nd instance still created and following warning is in logs:
04-12 17:01:17.041: WARN/ActivityManager(1289): startActivity called from non-Activity context; forcing Intent.FLAG_ACTIVITY_NEW_TASK for: Intent { act=android.intent.action.SEND typ=image/jpeg cmp=.FileUploader bnds=[0,628][480,723] (has extras) }
I’ve also tried to change line:
Context context = getApplicationContext();
with
Context context = this; // FileUploadServiceContext
But this doesn’t help.
Hmmm. I thought, maybe I launch wrong activity. Probably I should launch “parent” activity for my FileUploader.
For example: if I launch FileUploader from Gallery, then tap Home, then launch Gallery from Home screen again – I return to my Activity. So I wonder, maybe I need to launch Gallery/Camera from notification. But 2 questions:
-
how to determine what exactly was the parent of my activity?
-
what to do if that parent was closed by the system?
Well, the only way I found to implement this is:
This is not 100% of what I want (eg. I can’t return to “parent” application) after resume, but is better than nothing.