0

我正在尝试为我的项目创建应用程序快捷方式。我的项目涉及围绕少量活动的片段。使用的应用快捷方式意图PersistentBundle

这是我的意图的样子:

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;
}

解决这个问题的方法是序列化和反序列化到 String 并返回到 Class 对象。我尝试使用 ByteArrayOutputStream 进行转换,如下所示:

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

然后像下面这样转换回来:

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();

但是,这似乎不起作用。我正在将 MyFragment.class 转换为字符串并返回。但它会引发异常,更具体地说(Exceptionjava.io.StreamCorruptedException:无效的流标头:EFBFBDEF)我在这里缺少什么?

4

0 回答 0