I'm looking for the Java boilerplate for calling my installable app from my instant app. I found something coded in Kotlin on GitHub here https://github.com/googlesamples/android-instant-apps I'm trying to translate and get to work. I'm surprised the Java for this isn't provided somewhere, at least I haven't found it. Currently I'm just providing a link to my installable app on Goggle Play, which the user would have downloaded the instant app from in the first place. A bit cruder than having a nice window open over the instant app.
1 Answers
Here are two commands of Java code which will open a window in your instant app to your installable app on Google Play. I translated from Kotlin code I found deep in the sample apps on GitHub here: https://github.com/googlesamples/android-instant-apps.
Kotlin original with Java errors:
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import com.google.android.gms.instantapps.InstantApps
Java:
import com.google.android.gms.instantapps.InstantApps;
import android.content.Intent;
import android.net.Uri;
Intent postInstallIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://install-api.instantappsample.com/")).
addCategory(Intent.CATEGORY_BROWSABLE);
InstantApps.showInstallPrompt((Activity)getContext(),//'this' if calling from main
postInstallIntent,7,"InstallApiActivity");
or
Intent postInstallIntent = new Intent(Intent.ACTION_MAIN,
Uri.parse("https://your.package.name/")).
addCategory(Intent.CATEGORY_DEFAULT);
InstantApps.showInstallPrompt((Activity)getContext(),postInstallIntent,7,null);
Various parameters work for this, although the second is closer to what I discovered here https://developer.android.com/topic/google-play-instant/getting-started/instant-enabled-app-bundle#java. Bonus code, Java instant app flag:
boolean isInstantApp = InstantApps.getPackageManagerCompat(getContext()).isInstantApp();
Did I mention how much I hate Kotlin? Note this will crash the app if Google Play is disabled on the user device. I found this out the hard way, spending hours trying to solve a 'could not find activity' error after disabling Google Play to test what my app page looked like on a browser. If you don't disable the Google Play app first, searching for 'Google Play' on a phone browser automatically bounces you to the app, at least it does for me.

- 4,389
- 5
- 35
- 50