tags:

views:

171

answers:

1

I want to add home screen shortcuts to individual chat rooms, in my app. Here's my code to do so:

Intent roomIntent = roomIntent(room).putExtra("shortcut", true);

Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, roomIntent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, room.name);

Parcelable resource = Intent.ShortcutIconResource.fromContext(this, R.drawable.icon);
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, resource);

setResult(RESULT_OK, intent);       
finish();

When I go to add the shortcut to my home screen, I get a Force Close, not on my own process, but on com.android.acore(!). I've run the debugger and verified that my code gets executed all the way to the call to finish().

If I do this instead for the EXTRA_SHORTCUT_ICON:

intent.putExtra(Intent.EXTRA_SHORTCUT_ICON, R.drawable.icon);

It works fine and places the shortcut, and the shortcut behaves correctly -- but of course the shortcut has the stock Android icon, not mine, since this isn't the proper way to specify the icon.

When I look at the source code of other apps that have done this, and at the one example of it in the official Android reference area, my code looks identical. My icon's a standard 48x48 png that I use for the app's main icon, without problems. I've verified this problem on an emulator running stock 1.6, haven't tested other versions.

I have no idea what I'm doing wrong. Any ideas?

+1  A: 

The Javadoc for ACTION_CREATE_SHORTCUT says that you should use EXTRA_SHORTCUT_ICON_RESOURCE for Intent.ShortcutIconResource objects, rather than the EXTRA_SHORTCUT_ICON key you're using, which is used for directly placing a Bitmap into the Intent extras.

You should probably also file a bug for the crash on the Android bug tracker, as getting something like this wrong shouldn't bring down acore.

Christopher
Yes, of course. I kept comparing my code with other people's code, yet missing that change.It was different because I copy/pasted the shortcut code originally from another app of mine, which actually does place a Bitmap directly into the extras.THANK YOU!
Klondike
Glad it works! :)
Christopher
I followed your advice and filed a bug:http://code.google.com/p/android/issues/detail?id=5791
Klondike