Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
678 views
in Technique[技术] by (71.8m points)

android - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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'

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...