Android AppWidget Configuration - Start in new tas

2019-04-11 16:12发布

问题:

I've searched and searched and searched for this!

So I've got an app widget and it has a configuration activity that I can open when pressing a button on the app. The scenario is:

Had the app opened.

Closed the app with the home button.

Selected to add my widget

I have configured the widget.

Placed on my home screen

Then open the configuration activity again with the button on the widget.

Cancel the new config by pressing back will put me back into the app.

When pressing back I want to just return home.

Basically what I'm asking is. How do I start the configuration activity in it's own task/stack?

I've looked into intent filters but I'm just not quite sure, or maybe it's something to do with the package it's in, or maybe it's just not possible!

I suppose it may have something to do with the intent I use to launch the config activity

 Intent configIntent = new Intent(this, Configuration.class);
 configIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);      
 remoteView.setOnClickPendingIntent(R.id.config, PendingIntent.getActivity(this, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT));

Perhaps because I launch it with 'this' as the context, it will always start in my applications stack... but the pending intent api is: PendingIntent API 1 "Note that the activity will be started outside of the context of an existing activity"

So yeah I'll stop talking now as I just end up going in circles!

EDIT

So tried android:launchMode="singleInstance" in the manifest like was stated. This worked however it stops the 'startActivityForResult' behaviour working correctly. (which is the whole reason for a config activity) Get the error:

WARN/ActivityManager(59): Activity is launching as a new task, so cancelling activity result.

So still haven't found a solution.

回答1:

Ok sorted it :-) needed:

android:taskAffinity=""

in the manifest, setting the task affinity to an empty string allows for the activity to start in it's own stack, as it is not 'affiliated' with the rest of the application.

UPDATE

I have changed the task affinity to: android:taskAffinity="com.my.package.alternative.task" as each time I launched the activity it was showing up multiple times in the 'history'. So it now starts in it's own stack but is shared with other instances of the same activity.

Also need to add the Flag Intent.FLAG_ACTIVITY_NO_HISTORY to your intent :-) this stops your getting your application multiple time's in the history when you 'press and hold' the home button.

UPDATE

I've noticed FLAG_ACTIVITY_NO_HISTORY wasn't doing what I wanted, I've removed it and added:

android:excludeFromRecents="true"

into the activity tag in the manifest as well. The activity now behaves like I want :-)

Got this answer from the following link trail: Tasks & Back Stack | Managing Tasks | Affiliation Tag



回答2:

Try to put android:launchMode="singleInstance" for an activity of the app in AndroidManifest.xml