Using startActivityForResult across Android applications

时光怂恿深爱的人放手 提交于 2019-12-31 03:27:06

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9556206/using-startactivityforresult-across-android-applications

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!