I’d like to add a button to my Android app that acts as a link to another app. If the user has already downloaded it, the app launches; otherwise, it opens the Google Play Store with that app listed. I have access to both apps, and I’m wondering how I could set this up without using JavaScript redirects on the server.
<intent-filter android:autoVerify="true"> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <data android:scheme="http" /> <data android:scheme="https" /> <data android:host="example.com" /> </intent-filter>
Handle the deep link in the navigation graph or MainActivity
Configure the .well-known/assetlinks.json file on your server (see documentation).
fun openApp(context: Context) { val url = "https://example.com/" // your deep link val packageName = "com.app.example" // target app package name val intent = Intent(Intent.ACTION_VIEW, url.toUri()).apply { setPackage(packageName) // Only this app can handle the intent addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) } try { context.startActivity(intent) } catch (e: ActivityNotFoundException) { // Google Play Store fallback val playIntent = Intent( Intent.ACTION_VIEW, "https://play.google.com/store/apps/details?id=$packageName".toUri() ) context.startActivity(playIntent) } }