简体   繁体   中英

Android Instant Apps and use of App LInks

It looks like right now that Android Instant Apps are supported in Android 5.0 or later. However, App Links (which I understood that Instant Apps depend on) are only supported in 6.0 or later. I've searched online but couldn't find clear answer on this.

In general it looks like we'd want, to support Instant apps, to use app links to navigate between activities in different feature modules, but also need in most cases to use those modules to build installable apk that works on versions below 5.0
Does this mean that code needs to check the API level and use different approaches depending on version (eg calling startActivity with explicit intent if < 5.0)?

This is the info I've found in the Instant Apps documentation :

Both your instant and installable versions of your app must implement the Android App Links feature introduced in Android 6.0 . App Links provide the primary mechanism for connecting URLs to discrete activities within your app.

and

an instant app cannot launch an activity in another feature directly; instead, it must request the URL address that corresponds to the other other feature's entry-point activity.

and then from https://developer.android.com/topic/instant-apps/index.html

Android Instant Apps supports the latest Android devices from Android 5.0 (API level 21) through Android O

Android App Links just provide a way for Android system to uniquely associate your http deep-links with your application (without showing the disambiguation dialog for the user to select which app to open the link). It doesn't give you any new API to start an activity. Thus you will need to call startActivity in any case. You just need to use an implicit intent if you want to open an activity belonging to another Instant App feature module.

For navigation inside of the same feature module (or in case your Instant App consists of just one base feature ) an explicit intent could be freely used.

It looks like right now that Android Instant Apps are supported in Android 5.0 or later. However, App Links (which I understood that Instant Apps depend on) are only supported in 6.0 or later

Yes, that's true. But the Instant App supervisor (which is installed internally by the Google Play Services and used to run Instant Apps on Android before 8.0) will ensure that app links registered to your verified Instant App domain will be forwarded directly to your Instant App.

Does this mean that code needs to check the API level and use different approaches depending on version (eg calling startActivity if < 5.0)

Yes, if you want to be 100% sure your user won't be shown a disambiguation (aka "chooser") dialog like this , while browsing between the activities of your app (and most probably you would like to prevent such a weird user experience). If you use dependency injection, you can have an interface used for navigation in your app, and then different implementations for the installable and the instant app.

interface Navigation {
   void startActivityFromModuleA();
   void startActivityFromModuleB();
   …
}

class InstallableAppNavigation implements Navigation {
   public void startActivityFromModuleA() {
       // explicit intent
       Intent intent = new Intent(context, ActivityFromModuleA.class);
       context.startActivity(intent);
   }
   …
}

class InstantAppNavigation implements Navigation {
   public void startActivityFromModuleA() {
       // implicit intent
       Intent intent = new Intent(Intent.ACTION_VIEW,  
               Uri.parse("https://your.app.com/moduleA/smth"));
       context.startActivity(intent);
   }
   …
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM