The FULL Deeplinking Guide With Jetpack Compose! - Android Studio Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys and welcome back to a new video in this video I will show you everything about deep linking in Jetpack compose that you need to know first of all what is a deep link a deep Link in the end is a link that takes you to a specific destination of your app so you can think about that for example as if you would have the Instagram app and in the Instagram app you have a bunch of posts and if you then go to your browser and you click on a link that would open a specific Instagram post then with a deep link the app will open that post instead of the browser will open that post um in the yeah just as website so that will be one example but another example would be that your app shows a specific notification and if you click on that notification it doesn't just launch your activity instead it actually takes you to a specific destination to a specific screen of your app passing some data if you want to and as you can see here what we will build is a very simple two screen app we're going to have a starting screen which just takes us to the details screen if we click that and then we can see okay the ID is -1 so this is just some kind of data that we want to attach here clicking on this button won't attach any ID at all so we just set it to the default which is -1 but the interesting part is now if we close our app go to my website here I now added a button to actually open our app so if we click in our browser on this open app button you will see that our app will open with some data that we attach which is the ID 999. so that in the end is a deep link and I will show you how you can Implement that how you can implement it with your own website how we can also start this from another app so I also prepared a little other app here which simply triggers our deep link so we can click that and as you can see it takes us to our other app again and this time passing a different ID here so yeah you will learn both these how you can launch this from another app also yeah that would be the same approach how we would also launch your own activity and deep link using a notification for example or uh yeah just launching that from your web browser so I am here in an anti-japa composed project all you really need here to follow is this dependency here navigation compose that is of course needed to implement navigation and deep linking therefore if you have that we can go to main activity and we can create our simple nav host here we just used to define different screens where we want to navigate between we need a nav controller for that which you can simply get with remember nav controller we don't need to pass anything here we pass our nav controller we don't want to pass a graph instead we want to pass a start destination which I will simply set to home here and I'm here in this block of code we can add two composable blocks which each stands for like one specific screen on the one hand as I said we have the home screen which is just very simple let's add a box and a button here so the modifier could be modifier fill Max size and we want to align the content in the center of the box and in here inside of the Box we add this button I talked about when we click this button we simply want to navigate to our second screen so we can say nav controller navigate the route of that second screen will simply be called detail and the text of that button will be yeah to detail so and then below this home composable we want to add our detailed screen which will also accept these deep links so we are composable the route will be detailed and normally if we were to pass some navigation arguments here we would do this here in this route using something like um yeah like a d and curly brackets you can do that if you also want to be able to pass this ID while navigating here to this screen so if you want to pass it directly here but we only care about this ID in this case if we open this screen from a deep link so what we instead want to do is we want to add deep links so we simply add a list of nav deep links here we can get this block of code here to configure that and in here we have a bunch of options as you can see so we have a Yuri pattern an action and a mime type we only care about Yuri pattern and the action with this Theory pattern we can specify well the pattern for the URL where this deep link should trigger for so in my case that would be a https plcoding.com because that is my website and I want to make sure that when this Yuri is triggered by a deep link that our app is one of the apps that can that can handle this deep link and open the app with a corresponding ID that we pass and here we can now pass this ID so if we open https pillcoding.com 3 for example then the Deep link will detect that it satisfies that link and it will open our app with the corresponding ID that we passed in this URL we then also want to pass an action which is simply intent action view because the thing we want to do with that deep link is just to view some content for example if we take the like the Instagram example again then if you open such a URL for example instagram.com post slash any ID then you want to view that post so that is the actual action we want to perform here we then also need to pass some arguments so we specify an arguments block where we pass a list of nav arguments and here we only have our single argument which is ID of course and we can configure that so we can set the type of that argument which is simply nav type Dot intype and we can set the default value to -1 so if we don't attach any value for that it'll simply be -1 which will for example be the case when we navigate here since we don't attach any D to the screen cool that's it for the configuration of that composable we now of course need to also build it which is very simple we simply add a box again and here we just want to Center text that says hey that's your ID that you passed to this screen so we can say content alignment is Center we get our backstack entry here which contains the argument so we can say Val ID is equal to entry arguments get integer because we get an integer of course and the key of that is ID inside of our box we will say we have a text and the text will simply be the ID is and we pass our ID and that's already it for our screens of course right now that won't be enough to make our deep link work so we need to distinguish between explicit and implicit deep links here it's similar to intents in the end so an explicit deep link would be that we specify an exact application that will satisfy this deep link so if we are inside of our app and we for example want to launch our deep link inside our app then that would be an explicit deep link and that would work without issues however very often as in our case we want an external application to launch or a deep Link in this case the browser for example or any other app which I will also show you here and then just doing it like this won't work because in that case we also need what is called an intent filter we specify these intent filters in our manifest and an intent filter as you can see here is in the end just something that specifies hey our application can be launched by another application in these specific scenarios so for example we can say intent filter declare this here and in here we can declare three different types of properties on the one hand we have the data property which is needed here we can specify the host so that again is just our URL we say https actually not https we don't need that here just plcoding.com in this case and we then also specify this scheme which now would be https like this so that's just the same as we did here in our composable just that this is yeah composed only that compose knows which screen should open for this URL and this here is mend for other applications so that we really declare our app to be openable from other apps we then also need categories with these categories we can yeah Define some extra options so we need to only pass a name here and on the one hand we care about this default category which kind of marks this um our activity here to be launchable by default so that means even if the other app does not explicitly specify that our activity should be launched and just does not specify any activity at all then with this category we make sure that our app can still be launched and we also want to specify another category which is browsable which just means hey this deep link can be open from a browser and we also want an action that we attach the name of that will be action view just as we specified in order composable as well and now we have successfully specified a deep link or rather an intent filter that matches our deep link we could also copy this and sometimes you want this you can paste this again and also make this work for just HTTP so in my case only https will be relevant but depending on your use case you might want to have both these variants and if we Now launch this app on our device and take a look here and then right now we could of course navigate here and we get -1 as the ID that is not surprising but Android Studio actually has a built-in tool that allows us to easily test d-plex and we can reach this tool going to tools app links assistant then this window will open up and down here at point four we can test our Apple links so that will simply fire this intent that will launch our app with the given URL so here for example we could say one two three we can say run test and you can see that my app actually launches with that corresponding deep link so we get the ID here inside of the app however depending on the Android version this might not work this easily as you can see here on point three we can associate a website and I think starting from Android 12 we actually need to do that so if you want to be able to open your app from a website then you need to own that website so that is a change Google made and to be able to to tell Google hey that's my website and I want to be able to launch my app from that website we can open this digital asset links file generator and in here we first of all need to specify the site domain which is simply my website um you won't be able to follow this with my website obviously because you you don't own it but in case you have your own website I will show you how you could yeah kind of tell Android hey you own that website you specify that you specify your application ID which is already entered by default you don't need to tick the support sharing credentials between app and website that is used if you have some kind of login and you then um yeah into your website the user clicks on a link in your website and is logged in on that website then those credentials will also will also be shared with your app so that the user won't be won't need to actually enter these credentials in the app but we don't have any login here then you need to pass a signing config since we use the debug build here we just use the debug config but if you then publish your app you actually need to set that up with a specific keystore file obviously which you use to sign your app you can then click generate a digital asset links file and this is the piece of code you now need to copy then you go to your website and you need to log in to that with SSH ssh is basically just a tool to I mean remotely control a server if you have a website you will probably have such SSH access in my case yeah that's just the UL I need to access that will differ for your server you then press enter here in your terminal you'll log in with your password and then we are in here I can clear this I want to navigate into my specific domain and in here yeah we just have all these files that I have on my domain and what we now need to do is if we take a look here to complete associating your app with the website save the above file to this specific link so we need to go into dot well known and then create a file asset links to Json in our website root directory or rather in our websites well-known directory so we need to take this code here and copy it go to our terminal cdn2.well known and in here we need to create a file called asset links so we can do this with a nano that's just a text editor for the terminal so asset links.json I already have this file so I will yeah I will delete this very quickly um asset links are Json so we can create this from scratch like this and then we can simply um paste our code that we got from Android studio right here we can save this hitting Ctrl s and then Ctrl X to get out of this and now we actually made sure that this file is accessible at this specific domain if we then click link and verify you can see we get both these check marks I'm actually the first one is a bit weird because we haven't added this Auto verify yet so it seems like it still checks that I will show you what that means but the second one is the important one which just means that it can't find that entry on our website what this Auto verify is about is here in our intent filter oh and actually it did automatically add that but I did not add this seems like yeah Android Studio added that so Auto verify just means that it will automatically verify with our website that we actually own this and you need to add that if you actually yeah want your app to be usable on at least Android 12 plus which I guess you you want so let's Now launch this app again on our device and I also just deleted the other app I have so we now only have this single app that will satisfy our deep link if we go to our browser and we click open app then we can see that our app will actually open with the corresponding ID and in our browser yeah that's in the end just a link you can see it will open up fieldcoding.com 999. and that will work just fine however what happens if we now want to also launch this deep link from another app so not from our browser but from another app that we might own then I will also show you how to do that and for that I prepared another project here which is just yeah again an empty jetpack compose project here you don't need any dependencies and in here we will just set up a very basic box again with a button so we fill the whole size and we make the content aligned to Center and in here we just say we have a button when we click this button we want to launch our other app with a corresponding ID and the text will be trigger deep link in here the way we will do this is we need a so-called pending intent with these pending intents yep these are also what you actually need to attach to a notification for example and if you construct that so if the user Taps the notification the pending intent will fire and in this case it would simply launch our specified screen with a specified ID for pending intent we first of all need to create a normal intent so we can just do that here we need to specify an action here which is intent action View and we need to pass the application context to construct that intent or actually not the application context we don't need this year since it's an implicit intent we just need to attach the Yuri that we actually want to open so our URL so we can say yuri.powers oops not that one Euro powers we say https plcoding.com and we open any ID here after that we can construct our panning intent by simply saying task stack Builder dot create here we need to pass our application contacts and we stay that run so with this test stack Builder we can create our own type of panning intent that will open our other app we first of all want to add this next intent here so we want to kind of Link this intent to our panning intent with this at next intent with parent stack so we pass this and then we can say get pending intent to actually construct that panning intent we need to specify a request code so in case we get some result of that which we don't get here and we need to pass some Flags we want to pass panning intent flag update current so if there is already a pending intent then this one will update the existing one or so we're going to link these flags I want to say pending intent flag immutable that is just required on Android 12 and onwards I think or 11 not sure anymore you can see if we get a warning um okay it's actually it's required from Android 11 I think but it exists starting from API level 23 so we can simply say Alt Enter surround this with this per with this version check and simply if we are below this version we can paste this here again just without this flag cool so now we have our pending intent and all we now need to do is we need to say pending intent but sent to send that intent and if we Now launch this app taking a look here in our emulator or my device rather and then we get our screen with trigger deep link and if we now click this then our other app will open with the corresponding ID and those yeah that is basically everything you need to know about deep links in Android of course for XML it's a little bit different if you have an XLR project you actually retrieve your deep Link in the on new intent function of an activity so here we here you would then get this intent that was passed so in this case this one here and then you manually to parse this ID from that from that um yeah intent that was sent but in the end it's very similar you also need to specify stuff in the Manifest and then yeah you can launch your your app from other apps you can launch it from the browser and I hope that now helped you if it did then you will definitely also love my more advanced Android courses which you can find Down Below on my website where I can just go much more into detail build larger projects than I can do here on YouTube and apart from that if you have not subscribed to this channel yet and you don't want to miss two regular Android videos every single week then now it's a very good option to do that so hit subscribe don't miss any more videos and apart from that I will wish you an excellent rest of your week see you back in the next video bye bye foreign
Info
Channel: Philipp Lackner
Views: 38,467
Rating: undefined out of 5
Keywords: compose, jetpack, philip, deeplink, website, button
Id: z6VlP0o_sDc
Channel Id: undefined
Length: 19min 17sec (1157 seconds)
Published: Sun Oct 30 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.