How to open Android app if installed, otherwise redirect to Google Play (no JavaScript)


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.

0
Apr 30 at 10:18 AM
User AvatarStanisław Olszak
#android#android-deep-link

Accepted Answer

Open Android app if installed, otherwise redirect to Google Play

Required steps for target app:

  1. Add an intent filter for deep links:
<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>
  1. Handle the deep link in the navigation graph or MainActivity

  2. Configure the .well-known/assetlinks.json file on your server (see documentation).

Requirements for devices with a button:

  • Use a function to open the app with a fallback to Google Play:
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) } }
User AvatarStanisław Olszak
Apr 30 at 10:18 AM
0