1

I'm implementing an option for sharing content from my app. When the user presses the share button the following code is executed.

public static void openShareIntent(Context context, String text, Wish wish)
{
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");       
    share.putExtra(Intent.EXTRA_TEXT, text);
    share.putExtra("share_wish", wish);

    startIntent(context, share);
}

I'm setting one special extra for my Intent, that is object wish witch implements the Parcelable interface.

This object contains some extra information. I want to use this information if the user selects my app (current app actually sharing content) from the available apps for sharing text/plain.

But the problem is that all other popular apps (Facebook, Twitter, ...) and built-in apps (Messaging) crash when I include my Parcable object. It's not my applications that crashes, other apps are throwing quit unexpectedly error.

When I call my SharingActivity with some extra name that is not known to this Activity, it does not crash. It just ignore that extra.

Wish object source code

Am I doing something wrong or what? Is this not possible because other apps don't know my Wish object?

pzagor2
  • 709
  • 10
  • 24
  • 1
    Post your logcat. There's a good chance your `Parcelable` implementation is incorrect, so the other apps crash when attempting to unmarshal the object. – 323go Mar 07 '13 at 14:52
  • are **text/plain** and **Parcelable** same..? I don't think you can do that.. – ngesh Mar 07 '13 at 14:54
  • Nothing show up in LogCat. I'm sure other app can't unmarshal the object, because they don't know it. But they shouldn't even try it. @ngesh I suspect the same. But i hope this is not the case. – pzagor2 Mar 07 '13 at 14:56
  • Take a look at this http://stackoverflow.com/a/2141166/262462 – Kuitsi Mar 07 '13 at 15:01
  • "nothing shows up in logcat": Unlikely, given that you say there's a crash. Any filters set? -- I don't see what "text/plain" has to do with Parcelable or why they should be the same. -- Extras are collected in a Bundle, so apps processing an Intent will most likely just look for the keys they understand, otherwise I would have made other observations in the past. -- Why not post your `Wish` code? – class stacker Mar 07 '13 at 15:02
  • The Parcelable is definitely implemented correctly, because I'm using it all over my app. And i get the correct data when selecting my application from the list. – pzagor2 Mar 07 '13 at 15:03
  • 1
    It doesn't matter whether Parcelables work within your app. Once you send the Intent out of your app, you're crossing process boundaries, and you need to provide proper AIDL so the other process can unmarshal it. Without checking, I seem to remember that Android attempts to unmarshal the extras as the Intent is received, so it could be failing right there. – 323go Mar 07 '13 at 15:06
  • BTW it's not my application that creases. It's other applications. – pzagor2 Mar 07 '13 at 15:09
  • Have you played around with different `describeContents()` return values? -- Yes it's clear that other applications crash. Maybe they consider your Parcelable to be something else e.g. because of the describeContents value you have chosen. – class stacker Mar 07 '13 at 15:16
  • @ClassStacker i did not. I decided to put the object in Bundle as suggested by CommonsWare, and everything is working as expected. – pzagor2 Mar 07 '13 at 15:42
  • Certainly an excellent choice. – class stacker Mar 07 '13 at 15:47

2 Answers2

1

But the problem is that all other popular apps (Facebook, Twitter, ...) and built-in apps (Messaging) crash when I include my Parcable object.

Never pass a custom Parcelable object to an app that lacks your Parcelable class definition.

Is this not possible because other apps don't know my Wish object?

Correct.

Instead, pass an identifier (string, int, whatever) that SharingActivity can use to find your Wish from your central data model.

Or, instead of creating a custom Wish, use a Bundle to represent the "wish", as Bundle has a common class definition across all apps.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

You could just put your data in a bundle and send it with intent as well. eg:

Bundle b = new Bundle();
b.putParcelable("object_key", yourObject);
shareIntent.putExtra("bundle_key", b);
Sreenath
  • 1
  • 1