How do Android apps implement the share button?


Question

I was wondering how Android applications and Android implements the share button and share mechanism. For instance, when I hit the share button on YouTube, a list off apps to share to pops up. How is this list determined? Is there a specific "share" intent, and then all spots capable of sharing the type of intent provided in the intent extras show up? What is the technical name of the design pattern used to implement this? Thanks.


Answer

There are two parts to this: the mechanism for the communication apps to say they can share things, and the mechanism for the apps with content to share. I'll deal with them one at a time, but first a quick note about intents.



Intents



The intent in Android is what's used to start an app. It can work like a shortcut, or like a Windows file association. The intent can specify the name of the app to start, but it doesn't have to. It can also specify the action to perform (e.g. "open your main activity", "view this URL", "create a calendar entry") and the MIME type of the object it will act on (e.g. that the thing you want to view has type image/jpeg). Some names of actions are specific to a particular app, but many are standardised by Android to allow apps to interact.



Intent filters and communication app



Each app specifies intent filters to say what kind of actions it can perform. e.g. an image viewer app would register an intent filter for "view this URL" where the MIME type is "image/*"; a web browser would register an intent filter for "view this URL" where the URL scheme is "http" or "https". To be able to share things, an app registers an intent filter for the "send" intent, either with a specific type (e.g. if it can only share photos), or for any type.



Content app



The app that wants to share some content creates an intent with the "share" action and a URL pointing to the content to share. (This is often a "temporary" URL that gives the receiving app temporary access to the file in the content app's private storage, or to a stream generated by the content app.) It doesn't specify the name of an app to handle the intent. Then, it gives that intent to the Android framework and asks Android to display the chooser dialog. Android compares the intent with the intent filters from all installed apps, and creates the list and shows it to the user. When the user picks a particular app, Android starts that app and passes it the intent. The app uses the URL to get the content from the original app.



New in Marshmallow: Direct Share



In Marshmallow, there's another mechanism called Direct Share. This supplements the intent mechanism so that you can choose people or groups to share to directly from the chooser dialog, skipping the extra step of "who would you like to send it to". This works slightly different: the communication app has to provide a service. When the app with the content asks for a chooser, Android starts the service for each relevant app. The service returns a list of relevant share targets, and Android assembles all those separate lists into the list that gets shown in the dialog.


Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes