Reputation: 111
What I would like to do is the following:
So far I am able to populate and present a list with all the shortcuts, using
getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_CREATE_SHORTCUT), 0);
. Upon selecting a shortcut, I start the ACTION_CREATE_SHORTCUT
intent to customize the shortcut parameters - it presents the proper UI and seems to work. I use this code to start the intent:
ActivityInfo activity = resolveInfo.activityInfo;
ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
Intent i = new Intent(Intent.ACTION_CREATE_SHORTCUT);
i.addCategory(Intent.CATEGORY_DEFAULT);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
i.setComponent(name);
startActivityForResult(i, 1);
Here is my onActivityResult:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==1 && resultCode == RESULT_OK) {
try {
startActivity((Intent) data.getExtras().get(Intent.EXTRA_SHORTCUT_INTENT));
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Success!!", Toast.LENGTH_LONG).show();
finish();
}else{
Toast.makeText(getApplicationContext(), "Fail: "+resultCode+" "+resultCode, Toast.LENGTH_LONG).show();
}
super.onActivityResult(requestCode, resultCode, data);
}
Now, the problem is that the onActivityResult
always gets triggered immediately after startActivityForResult
with requestCode=0, resultCode=0 and with no data. It does not trigger when the ACTION_CREATE_SHORTCUT
activity actually ends. I really don't get it. I think that after the activity ends it should return the requestCode I sent it and the data intent, containing the Intent.EXTRA_SHORTCUT_INTENT
which I could then use somehow to actually start the shortcut.
The second part of the question is how do I actually store the necessary information for the shortcut the user selected, preferably in SharedPreferences
, so I could later execute this shortcut with the specific parameters. I couldn't find any example of this.
Any help would be much appreciated! Thanks!
Upvotes: 2
Views: 1244
Reputation: 111
More then 2 years later, here is the answer to my question:
The proper functioning of the startActivityForResult
/onActivityResult
system obviously depends on both the calling and the called Activities being part of the same Task. Therefore any action which would cause the two activities to be launched in separate Tasks would break this functionality. Such actions include setting any exclusive launchMode
for any of the Activities in the AndroidManifest.xml
or using flags such as Intent.FLAG_ACTIVITY_NEW_TASK
when launching any of the two Activities.
Upvoted the answer of user2427931 for the Intent.parseUri()
solution.
Upvotes: 3
Reputation: 21
I had the same behavior when my calling activity had launchMode="singleInstance". Do you have that defined as well?
Regarding saving the Intents. You could turn the intent into an URI and save it as a string in SharedPreferences. You could then use Intent.pareseUri() once you have retrieved it.
//Erty
Upvotes: 2