not fairly Make your Android app a share goal. Obtain easy knowledge from different purposes with Hilt and Compose | by Nikita Grishko | Jan, 2023 will cowl the most recent and most present suggestion one thing just like the world. gate slowly in view of that you just perceive skillfully and accurately. will buildup your information adroitly and reliably
To raised perceive the sharing performance, let’s outline its use case. For instance, in case you are growing a social networking utility, you would possibly need to present the flexibility to share textual content or pictures. Actually, there are various extra purposes for this performance. Nearly all messaging apps, social networks, and even easy note-taking apps use this characteristic.
For a non-compositing app, there is a fairly simple information within the official Android documentation, however I’ve discovered that this is not fairly the identical for compositing apps. So let’s go and discover ways to do that step-by-step.
First, let’s talk about what applied sciences and libraries we are going to use. To maintain this text easy, I am assuming most of you’re conversant in Compose and all different trendy instruments. If not, I extremely suggest checking them out.
- Jetpack Compose is our UI toolkit.
- Compose navigation to navigate between composables whereas making the most of the infrastructure and options of the navigation part.
- Hilt is our DI framework. Right here it’s also necessary to know find out how to combine Hilt with Jetpack Compose
On this record, the Hilt is an non-compulsory part, however it should make your life lots simpler, so I like to recommend utilizing it.
First let’s replace our manifest. To mark our utility as a shared aim, we should specify <intent-filter>
part for our Exercise.
By specifying the intent filter part for our exercise, we let the system know that we’re able to course of the intent with the desired parameters.
So in our case these parameters will probably be:
<motion android:title="android.intent.motion.SEND" />
That is the default motion used to share some knowledge.
<knowledge android:mimeType="textual content/*" />
Right here we let the system know that we will course of any textual content knowledge.
<knowledge android:mimeType="picture/*" />
And that is serving to obtain a picture.
For a extra detailed description, learn the official information.
If you are going to launch your app proper from time to time share one thing, your app ought to seem in Android’s share sheet.
Now’s the time to replace our navigation graph, since we in all probability need to present a selected display that may course of shared knowledge.
Your navigation graph ought to appear to be this:
Fairly easy, proper? Identical to for normal deep hyperlinks, we specify the record of navDeepLink
. however as a substitute of uriPattern
we’re specifying motion
Y mimeType
that correspond to our intent filter within the manifest. The subsequent query is how can I entry the shared knowledge? Let’s look.
And naturally, let’s inject our ViewModel into Composable:
Our Intent is now out there to our ViewModel through SavedStateHandle with none extra work. To entry it, we have to use the important thing NavController.KEY_DEEP_LINK_INTENT. Hilt will do all of the magic for us.
The final step is to get the intent knowledge. So let’s implement our Intent.parseSharedContent()
operate.
As you possibly can see on this code snippet, it’s worthwhile to use the next methodology to entry the textual content knowledge:
val textContent:String? = getStringExtra(Intent.EXTRA_TEXT)
And to entry the picture knowledge, it is best to use:
val imageContent: Uri? = getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri
And that is it!
This characteristic, together with many others, was applied in my open supply challenge. So you should definitely take a look at the repository and Thanks for studying.
I hope the article nearly Make your Android app a share goal. Obtain easy knowledge from different purposes with Hilt and Compose | by Nikita Grishko | Jan, 2023 provides notion to you and is helpful for totaling to your information