GoogleChrome / android-browser-helper

The Android Browser Helper library helps developers use Custom Tabs and Trusted Web Activities on top of the AndroidX browser support library.
Apache License 2.0
687 stars 288 forks source link

how to close TWA #261

Open mohit1993 opened 3 years ago

mohit1993 commented 3 years ago

Hello,

I am not able to exit TWA's activity(WebView), i have a scenario where user needs to send feedback with phone shake gesture, i will listen sensor in native code with a service, i need to open a UI(Activity) for user where user can see feedback form , but i am not able to open new activity as TWA was already being launched and TWA's view is in foreground.

I have a custom android activity which have code to launch TWA : basically i don't have TWA's Launcher activity as my launcher activity in AndroidManifest instead i copied code from TWA's Launcher Activity to my android launcher activity to launch TWA. Even if i finish my only activity which launches TWA, TWA is still in the foreground. I need help here whether i should be able to close TWA Web view, or i should be able to launch a new activity on top of TWA. I am not able to use my only activity(Launcher Activity) as it is also in onPause state due to TWA's view.

public void launchTWA() {
        try {
            this.twaLauncher = new TwaLauncher(this);
            this.twaLauncher.launch(twaBuilder, customTabsCallback, this.splashScreenStrategy,
                    () -> this.browserWasLaunched = true, this.twaHelper.getFallbackStrategy());
            if (!WhiteBoardLauncherActivity.chromeVersionChecked) {
                ChromeUpdatePrompt.promptIfNeeded(this, this.twaLauncher.getProviderPackage());
                setChromeVersionChecked(true);
            }
            (new TwaSharedPreferencesManager(this))
                    .writeLastLaunchedProviderPackageName(this.twaLauncher.getProviderPackage());
        } catch (ActivityNotFoundException e) {
            finishActivity();
        } catch (Exception e) {
            }
    }
andreban commented 3 years ago

I am not able to exit TWA's activity(WebView), i have a scenario where user needs to send feedback with phone shake gesture, i will listen sensor in native code with a service, i need to open a UI(Activity) for user where user can see feedback form , but i am not able to open new activity as TWA was already being launched and TWA's view is in foreground.

Trying to understand better the issue. Is this something that only happens when using the WebView fallback? Only when rendering using the browser, or both?

lassellea-sfeir commented 2 years ago

Hi folks, I have almost the same use case. I have a foreground service running in the native part of my app while the pwa is running in a CCT instance. The Activity which have launched the CCT extends com.google.androidbrowserhelper.trusted.LauncherActivity

My need is to finish the CCT and exit the app, here is my code in my foreground service :

stopService(intent);
((Activity) LauncherActivity.currentContext).finishAffinity();
System.exit(0);

In the logcat I see the app being destroying, but the CCT is still open and the user can still interact with it

Can you help me please ? Thx

jykdesigns commented 1 year ago

Same. The TWA launches in foreground.... but there's no way of closing the TWA. Clicking back button sends it background. I actually want the app to close.

EiraGe commented 1 year ago

This seems similar to crbug.com/925539. unfortunately there is no way to close the foreground CCT. I'll mark this as feature request.

lassellea-sfeir commented 1 year ago

Thanks !