Search code examples
androidandroid-sharing

Share an object list externally using ShareActionProvider


I have a RecyclerView which needed to be sent/shared with external apps like WhatsApp etc. Using ShareActionProvider to achieve this. Have created a menu and inflated in the overflow area in ToolBar using SharedActionProvider.

sample code.

  public void prepareShareIntent() {

    shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Sample Text.");
    shareIntent.setType("text/plain");
    startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.share)));
}

If data sharing between internal activities, we usually pass objects/object list by serialising/ Parcelable or using gson , no issue here since we can easily parse the data in the receiving activity. But in case, if I need to share a list Object to external apps what should be done. Does the external apps interpret as only specific types?

Appreciate the possible approaches. Thanks in advance.


Solution

  • I have a RecyclerView which needed to be sent/shared with external apps like WhatsApp etc.

    That is not possible. First, you cannot pass a View to another process. Second, WhatsApp and other apps would not know what to do with it.

    If data sharing between internal activities, we usually pass objects/object list by serialising/ Parcelable or using gson , no issue here since we can easily parse the data in the receiving activity.

    You cannot convert a RecyclerView into a Serializable, a Parcelable, or JSON. You might be able to convert the model data that the RecyclerView uses into one of those things, but third-party apps (e.g., WhatsApp) may not know what to do with it.

    Does the external apps interpret as only specific types?

    Correct. The MIME type of the ACTION_SEND Intent needs to be the MIME type of the content shared via EXTRA_TEXT or EXTRA_STREAM.

    I recommend that you spend some time thinking about what exactly you expect WhatsApp to do when you share content with it, where that content is somehow tied to a RecyclerView. For example, if you want WhatsApp to send a screenshot of the RecyclerView to somebody, then you should be sharing a screenshot of the RecyclerView, not the RecyclerView itself.