Akshay
Akshay

Reputation: 815

Passing Serialized Objects for AppShortcut Intent Android

I am trying to create app shortcuts for my project. My project involves Fragments around few Activities. The app shortcut intent usesd PersistentBundle.

Here's how my intent looks like:

public static Intent getAppShortcutIntent(@NonNull Context context) {
    Intent intent = new Intent(context, MyActivity.class);
    intent.putExtra(MyActivity.EXTRA_FRAGMENT_TO_LAUNCH, getString(MyFragment.class));
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.setAction(Intent.ACTION_VIEW);
    return intent;
}

The way to go for this is serialization and de-serialization to String and back to Class object. I have tried converting using ByteArrayOutputStream like below:

   ByteArrayOutputStream bo = new ByteArrayOutputStream();
   ObjectOutputStream so = new ObjectOutputStream(bo);
   so.writeObject(object);
   so.flush();
   return new String(bo.toByteArray());

And then converting back like below:

String serializedObject; // This is what is serialized above
byte[] b = serializedObject.getBytes();
ByteArrayInputStream bi = new ByteArrayInputStream(b);
ObjectInputStream si = new ObjectInputStream(bi);
return (Class<?>) si.readObject();

However, this doesn't seem to work. I am converting the MyFragment.class to string and back. But it throws exception, more specifically (Exceptionjava.io.StreamCorruptedException: invalid stream header: EFBFBDEF) What am I missing here?

Upvotes: 0

Views: 256

Answers (0)

Related Questions