Pass Serializable Object to Pending Intent

There are known issues with putting custom objects into an Intent that is then passed to AlarmManager or NotificationManager or other external applications. You can try to wrap your custom object in a Bundle, as this will sometimes work. For example, change your code to:

Intent myIntent = new Intent(getApplicationContext(), 
                             AlarmAlertBroadcastReciever.class);
Bundle bundle = new Bundle();
bundle.putSerializable("alarm", alarm);
myIntent.putExtra("bundle", bundle);

and in AlarmAlertBroadcastReciever.onReceive():

Bundle bundle = intent.getBundleExtra("bundle");
if (bundle != null) {
    Alarm alarm = (Alarm)bundle.getSerializable("alarm");
}

If this does not work (and this should work on most Android versions/devices, but not all, especially very new ones), you will need to convert your Serializeable object into a byte[] and put the byte[] into the extras.

There are dozens of examples of how to do that on Stackoverflow.

Leave a Comment