2

I have written an application that has an <intent-filter/> so that other applications can start it using startActivityForResult(). When this activity is finished it has created an object that is like:

(Application A)

 public class MyObject implements Serializable {
      private String name;
      private String[] items
 }

And set's it on the result:

(Application A)

 getIntent().putExtra("Extra_MyObject", myObject);
 setResult(RESULT_OK, getIntent());

So the second activity that is recieving this intent has an issue, how does it know the name of the intent extra to receive and how would I create the object received and cast it back into a MyObject that is not part of of this application?

(Application B)

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == MY_REQ_CODE){
        if(resultCode == RESULT_OK){
                 // So here how do I know what the intent data name is i.e. "Extra_MyObject"
                 // and how would I know the structure to cast the object back to a MyObject that isn't part of this project?

        }
    }
}

Would I just mimic the MyObject class in the second application and cast it to that? or are there other options?

Blundell
  • 75,855
  • 30
  • 208
  • 233

2 Answers2

3

You could use a library like Jackson to serialize your object to a JSON string which you then you deserialize on the other end.

It's much more flexible and will eliminate the problem of inconsistent versions of your data being passed around if you decide to add for instance an extra field.

Also, the two apps no longer need to maintain a class in the same namespace or even call them by the same name.

And as a final note, you no longer need to publish a jar, only your documentation will suffice.

Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
  • That's a good idea actually. I can just create my own JSON object with a `getAsJson()` in `MyObject` and send it as a `String` on the `Intent` then I would only need to maintain an API with the structure of the JSON right – Blundell Mar 04 '12 at 16:08
  • yes. No need to create a getAsJson method and implement it yourself. Take a look at Jackson. It will take care of the serialization to JSON for you. – Savvas Dalkitsis Mar 04 '12 at 16:10
  • 2
    Note that Android has `org.json` classes built in. Jackson is cool, but the fewer dependencies you impose on third parties (i.e., whoever is consuming this result), the better off you'll be. – CommonsWare Mar 04 '12 at 16:39
  • 1
    There is no need for the consumer to depend on Jackson. They can parse the JSON in whatever way the prefer. I was suggesting the use of Jackson to produce the JSON only. – Savvas Dalkitsis Mar 04 '12 at 17:02
  • @SavvasDalkitsis: Ah, sorry, I misinterpreted your reference to Jackson. My apologies. – CommonsWare Mar 04 '12 at 17:49
1

how does it know the name of the intent extra to receive

By reading the API documentation published by the author of the first application.

how would I create the object received and cast it back into a MyObject that is not part of of this application?

The authors of the first application would also have to publish a JAR containing an implementation of MyObject, that the authors of the second application add to their project. And, the authors of the first application would either never have to change MyObject or at least make sure the serialization versioning stuff works.

Would I just mimic the MyObject class in the second application and cast it to that?

If by "mimic" you mean "have a class with the exact same class name, exact same package name, and exact same serialization logic", then yes, though I'm then concerned about your use of the term "just"... :-)

If MyObject has the structure you describe, consider just putting the String and String[] as individual extras, or put them in a Bundle and use that as an extra, to avoid trying to use custom Parcelable objects between apps.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Cheers Mark, the MyObject is more complex than that. Any recommended links for creating API documentation and publishing a jar? – Blundell Mar 04 '12 at 16:00
  • @Blundell: "Any recommended links for creating API documentation" -- ummmm... use a Web page? "and publishing a jar?" -- this is standard Java, unless `MyObject` has Android-specific hooks (e.g., needs `android.*` classes). In that case, I suggest creating an Android library project instead of a JAR. – CommonsWare Mar 04 '12 at 16:36
  • I meant more like a simple version of this : http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javadoc.html I'll go Google! – Blundell Mar 04 '12 at 17:31
  • @Blundell: That's just JavaDocs. With the right code comments, there's a doclet that can generate those. – CommonsWare Mar 04 '12 at 17:49
  • Yeah no worries, I'm being completely lazy. I was assuming I could find some websites that had javadoc templates or other API style templates that I could fill in, instead of generating the standard white and blue. – Blundell Mar 04 '12 at 17:55
  • @Blundell: Yeah, I assume there are custom doclets for that sort of thing, but I have zero experience with them. – CommonsWare Mar 04 '12 at 18:01