0

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?

Akshay
  • 806
  • 11
  • 26
  • "I am converting the MyFragment.class to string and back" -- a `Fragment` cannot be converted meaningfully into a string, `byte[]`, or anything else for passing across process boundaries. Beyond that, perhaps do not convert your `byte[]` into a string, and just use the `byte[]`. – CommonsWare Oct 23 '17 at 17:57
  • @CommonsWare refer this SO link: It is not possile to pass byte array in PersistableBundle: https://stackoverflow.com/questions/41245557/shortcuts-for-nougat-version/46894844#46894844 – Akshay Oct 23 '17 at 18:03
  • OK, that's annoying. You might run some instrumentation tests on your serialization code, to see if the `byte[]` you get back the round-trip conversion to a `String` matches the original `byte[]`. – CommonsWare Oct 23 '17 at 18:12
  • 1
    Also, FWIW, I filed [a feature request](https://issuetracker.google.com/issues/68145856) to get more types moved up from `Bundle` to `BaseBundle` and get them supported in `PersistableBundle`. – CommonsWare Oct 23 '17 at 18:19

0 Answers0