Cannot understand how to open a simple TWA inside an app (using AndroidX)
Asked Answered
G

3

9

I am trying to open a TWA inside my app and have researched for 2 days.

I've already managed to create a single TWA app, no fuss, just edit the manifest and a few things more.

Now I need to have my own app - let's say the app has a splash screen activity at first which then opens the TWA inside the app. Can I launch a TWA inside my app through a simple splash screen activity, for example?

I did try to use CustomTabs way, but it says it is deprecated and to use TrustedWebActivityIntentBuilder instead, but there is 0, I repeat, ZERO documentation on how to use that!

Android development documentation is horrible. Among other things, the documentation pointers are out-dated. (Read videos on their channel linking to guides that are no longer valid for what is discussed in the video itself)

The closest thing I found was this sample project. This uses a shocking number of deprecated things rendering the adaptation of that method into my app completely useless. It also makes use of a countless number of custom Classes/Helpers created just for that project, leading me to a never ending marathon of copy-pasting each one of them just to find out that inside that one there are more that need to be copied over to the project.

Ginsburg answered 31/8, 2019 at 22:27 Comment(1)
I suggest to document clearly what you've tried, quoting what the documentation states wrong (i.e. did step X, got error Y). You might also want to review any related issues in the Chromium bug reports Ideally your post should be more technical problems than disappointment in documentation.Marcille
G
0

After a lot of trial and error as always I've managed to launch a TWA inside the app. Note that this is not like a WebView, it merely starts an activity on your app's stack.

The following is in my Activity's code:

final static String PACKAGE_NAME = "com.android.chrome";
final static String BASE_URL = "https://your.website.com";
CustomTabsClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_launcher);

    CustomTabsClient.bindCustomTabsService(this, PACKAGE_NAME, getConnection(BASE_URL));

}

private CustomTabsServiceConnection getConnection(final String url) {

    return new CustomTabsServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mClient = null;
        }

        @Override
        public void onCustomTabsServiceConnected(
            ComponentName name,
            CustomTabsClient client
        ) {

            final Intent intent;
            final CustomTabsSession customTabsSession;
            final TrustedWebActivityIntentBuilder intentBuilder;

            mClient = client;
            mClient.warmup(0);

            if ((customTabsSession = mClient.newSession(new CustomTabsCallback())) == null) {
                return;
            }

            intentBuilder = new TrustedWebActivityIntentBuilder(Uri.parse(url))
                .setToolbarColor(Color.parseColor("#ffffff"))
                .setNavigationBarColor(Color.parseColor("#ffffff"));

            intent = intentBuilder.build(customTabsSession);

            startActivity(intent);

        }

    };

}
Ginsburg answered 1/9, 2019 at 11:26 Comment(0)
R
7

To my opinion there is a simpler approach.

First: declare your TWA activity in AndroidManifest.xml like shown below. Note that it won't start by default because it doesn't handle the android.intent.action.MAIN intent, so you can implement your own main activity.

    <activity
        android:name="com.google.androidbrowserhelper.trusted.LauncherActivity">

        <!-- Edit android:value to change the url opened by the TWA -->
        <meta-data
            android:name="android.support.customtabs.trusted.DEFAULT_URL"
            android:value="https://YOUR_SITE_URL" />

        <!--
          This intent-filter allows the TWA to handle Intents to open
          YOUR_SITE_URL
        -->
        <intent-filter>
            <action android:name="android.intent.action.VIEW"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE"/>

            <!-- Edit android:host to handle links to the target URL-->
            <data
                android:scheme="https"
                android:host="YOUR_SITE_URL"/>
        </intent-filter>
    </activity>

Second: somewhere in your code start the TWA activity with the intent like this. You can also pass site url in the intent if you wish.

Intent intent = new Intent(this, com.google.androidbrowserhelper.trusted.LauncherActivity.class);
intent.setData(Uri.parse("ANOTHER_SITE_URL"));
startActivity(intent);

Also note the dependencies required to use TWA with AndroidX:

implementation 'androidx.browser:browser:1.0.0'
implementation 'com.github.GoogleChrome:android-browser-helper:ff8dfc4ed3d4133aacc837673c88d090d3628ec8'
Ruthannruthanne answered 23/9, 2019 at 20:24 Comment(0)
S
6

When launching the Trusted Web Activity from an existing Activity, the recommended approach is using TwaLauncher, from android-browser-helper. There's a demo for this use-case, but the implementation would be:

  1. Import android-browser-helper in the build.gradle:
dependencies {
    ...
    implementation 'com.google.androidbrowserhelper:androidbrowserhelper:2.0.0'    
}
  1. Launch the Trusted Web Activity from an Activity:
public void launchTwa(Uri uri) {
    TrustedWebActivityIntentBuilder builder = new TrustedWebActivityIntentBuilder(uri)
        .setNavigationBarColor(Color.RED) // Use the builder to customise.
        .setToolbarColor(Color.BLUE);

    TwaLauncher launcher = new TwaLauncher(this);
    launcher.launch(builder, null, null);
}

Comparing to other approaches:

Using LauncherActivity: Using LauncherActivity adds an extra level of indirection, which will introduce delays when launching the Trusted Web Activity from an existing Activity (eg: Activity X starts the LauncherActivity, which in turn starts the Trusted Web Activity).

Using androidx.browser directly: There's nothing wrong with this approach, but TwaLauncher encapsulates almost everything needed to handle that already.

Shipway answered 15/10, 2020 at 14:0 Comment(1)
Hi. if using this, i dont see any way to verify digital asset links so that the toolbar will be hidden. i am currently integrating this but somehow, it always says fail verification but the links test ok.Unnecessarily
G
0

After a lot of trial and error as always I've managed to launch a TWA inside the app. Note that this is not like a WebView, it merely starts an activity on your app's stack.

The following is in my Activity's code:

final static String PACKAGE_NAME = "com.android.chrome";
final static String BASE_URL = "https://your.website.com";
CustomTabsClient mClient;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_launcher);

    CustomTabsClient.bindCustomTabsService(this, PACKAGE_NAME, getConnection(BASE_URL));

}

private CustomTabsServiceConnection getConnection(final String url) {

    return new CustomTabsServiceConnection() {

        @Override
        public void onServiceDisconnected(ComponentName componentName) {
            mClient = null;
        }

        @Override
        public void onCustomTabsServiceConnected(
            ComponentName name,
            CustomTabsClient client
        ) {

            final Intent intent;
            final CustomTabsSession customTabsSession;
            final TrustedWebActivityIntentBuilder intentBuilder;

            mClient = client;
            mClient.warmup(0);

            if ((customTabsSession = mClient.newSession(new CustomTabsCallback())) == null) {
                return;
            }

            intentBuilder = new TrustedWebActivityIntentBuilder(Uri.parse(url))
                .setToolbarColor(Color.parseColor("#ffffff"))
                .setNavigationBarColor(Color.parseColor("#ffffff"));

            intent = intentBuilder.build(customTabsSession);

            startActivity(intent);

        }

    };

}
Ginsburg answered 1/9, 2019 at 11:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.