androidnightmaretrusted-web-activity

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


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.


Solution

  • 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);
    
            }
    
        };
    
    }