I want to create a homescreen shortcut to an inner activity of my app. This answer helped to get me started.
Here's the basic code:
Intent shortcutIntent = new Intent(context.getApplicationContext(),
StationMainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
shortcutIntent.setAction(Intent.ACTION_MAIN);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, stationData.getName());
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
Intent.ShortcutIconResource.fromContext(context.getApplicationContext(),
R.drawable.ic_launcher));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
addIntent.putExtra("duplicate", false);
context.getApplicationContext().sendBroadcast(addIntent);
Unfortunately, while it does work to provide an extra of type String the code appears to fail if I try to provide a serializable extra to the shortcutIntent.
shortcutIntent.putExtra("StationId", (String) stationData.getId());
does work. But
shortcutIntent.putExtra("StationData", stationData);
where stationData is a serializable Object does not. So right now I provide all fields of the object as string and recreate the object when the activity is called. This does work but it's cumbersome and dirty code.
Any idea why providing a serializable object does not work in this case? Thanks.
Update: Here's how I try to retrieve the Serializeable:
stationData = (StationData) intent.getSerializableExtra("StationData");
StationData looks like this:
public class StationData implements Serializable {
private String id;
private String name;
...
public StationData(String id, String name, ....) {
this.id = id;
this.name = name;
...
}
}