I'm seeing a strange behaviour only happening on android 12 devices.
In a Fragment of the app we tap to open a link in the browser like this:
Intent(Intent.ACTION_VIEW, Uri.parse(link)) and then startActivity(intent)
The link is to our own website which has the host verified.
What happens then is the MainActivity is destroyed and restarted and afterwards, the browser opens.
The app is based on a single activity with Fragments and has launchMode=singleTask
Devices running other than android 12 just open the browser and when going back the app is at the last Fragment visited - this is the desired behaviour and what I would expect as well.
A note on this is that the app handles deep links correctly. It also has the autoVerify enabled and the setup for the assetLinks.json is correct - the app open automatically when clicking a link.
My lines of thought is that the changes introduced around deep links and autoVerify must be causing this. If we swap the link for https://www.google.com it doesn't destroy the MainActivity and we can visit the website and go back to the app. Any other link with one of our hosts causes that behaviour.
Curious if anyone has experience the same. We are thinking in opening a webview inside to avoid this as a temporal solution but it wouldn't be ideal to do so.
I am showing a splash screen(TWA splash) when launching TWA, but I see a white screen for 4-5 seconds after the splash screen when launch the app after a delay, as my web code needs to load a lot more data before launch, I know my web code should not take too much time to load, but I want to handle this white screen with a loader on native end only due to some limitation.
I have not extended LauncherActivity(from android browser helper SDK) neither used it as a launcher in Manifest, I had copied code(whatever required) from this activity to my native activity Abc due to some conditional launch.
I am not able to figure out which activity is hosting web content now after the TWA launch. As my activity, Abc seems to be in an onPause state. so I can not use this activity to show loader. If I used a new activity to show a loader my web content does not load in the background, it starts loading only when web content is in the foreground.
I want to show loader when there is time to load web content, is there any way to do it?
Note: I have few redirects in my web URLs for launch.
The problem in this case is that the web application is taking too long to load.
The Splash Screen from the Trusted Web Activity will be started while Chrome is loading the page and will be automatically removed by the time the initial URL fires the pageload event.
There are several techniques you can use to improve the performance from your web application. A good place to get started is https://web.dev/fast/. One tip from your description is that the Trusted Web Activity should open the final URL and not have any redirects.
I have a meteor app running that can be added to homescreen on android. Doing this neatly installs the PWA. When I tap an URL in my system, the intent filter fires correctly and opens my app, but the thing is:
If my app is not open in the background, the intent fires correctly, opens my app and navigates to the correct URL.
If my app is open in the background the intent fires correctly and opens my app but doesn't navigate to the URL and the app just remains on the same page.
Any ideas as to how this is possible? Of course I want scenario 2 to also open the right URL directly.
I can't really control how the apk works on my phone because I believe chrome creates the apk when I use the "add to homescreen" function. I have setup the scope and start_url correctly in my webapps manifest.json. Perhaps I am missing a service worker I don't know about?
Unlike the answer to
Chrome Custom Tab does not go back to app
My app works correctly when pressing the x or using the back button. However, when the server redirects the user back to the app inside the custom tab the window does not close. The redirect is a javascript intent to deep link back into the app. If I use a browser and skip the custom tab the redirect works correctly and closes the browser window. I have tried several values for the launchMode and noHistory in the Manifest but it's not making any difference.
Skipping the Chrome tab and going straight to a browser is the only choice. The user experience with Tabs would be much, much better. The behavior seems to change with different Android versions and gets worse with Android 6 and 7. The only error I can find in the unfiltered adb logcat is:
12-28 23:58:50.580 3595-3713/? I/ActivityManager: Displayed com.modolabs.kand28/com.modolabs.kurogo.core.activity.LoginActivity: +117ms
12-28 23:58:50.590 32437-13513/? W/System.err: remove failed: ENOENT (No such file or directory) : /data/user/0/com.android.chrome/files/android_ticl_service_state.bin
This says that ActivityManager has started LoginActivity inside my app as it should but the LoginActivity window is behind the Chrome Custom Tab. If I complete the user action that invokes the redirect and then manually close the tab using the X, then I can get to the app and I see it is doing what it is supposed to do. My app is not getting focus to be put in the foreground on top of the tab.
Anybody else seeing this? Any suggestions?
I have a web app and on the Android I would like to display an alert describing how to add my app to the home screen. (Add it to "Bookmarks" and then "Add it to home screen" or "Add to shortcut in Home"). Then a icon will be displayed on the screen that opens my app.
But off course I only want this to show if the app is not added to the home screen.
Does anybody know how to do this?
Any input appreciated, thanks.
Yes, you can.
While technically a page open in Chrome browser tab can't directly check whether a home screen shortcut exists, the page's local data (localStorage, IndexedDB) is shared between home screen instance and browser tabs, so this can be used to communicate the existence of the home screen version.
Detect if the app is running from home screen
If it's ran from home screen, save this fact to localStorage
Profit! (by reading this from localStorage in any tab)
When the app is in standalone view (which is possible only when launched from home screen), the CSS media query (display-mode: standalone) will match. In Javascript you can read it using:
matchMedia('(display-mode: standalone)').matches
(BTW: the non-standard iOS equivalent of this is navigator.standalone, but iOS doesn't share state between home screen and Safari, so you're out of luck there).
However, instead of custom instructions I suggest meeting Chrome's criteria for "progressive web app" and let Chrome do the prompting for you.
You can't check a web app if its added to home screen on android. At least for now. (Chrome 67)
But you can tell if the app is running in standalone mode using display-mode media query.
Add this inside your <style> tag.
#media all and (display-mode: standalone) {
body {
background-color: yellow;
}
}
Then in your <script> tag.
if (window.matchMedia('(display-mode: standalone)').matches) {
console.log('display-mode is standalone');
}
Or this
if (window.navigator.standalone === true) {
console.log('display-mode is standalone');
}
You can check more from here.
The short answer is: from a Web Site you can't.
The longer answer is: from a Web site you might be able to get a hint in Chrome.
Chrome on Android two new features 1) Web App Manifest that describes what should be launched from the home screen and how it should look on the homescreen, and 2) Chrome now has an beforeinstallprompt event that will trigger for Web apps that we think are app-like and can be installed to the homescreen.
There are a number of criteria for the onbeforeinstallprompt event to fire which might make it an "ok" heuristic (although I suspect not).
The event only fires if:
The site has a manifest, is on https and has a service worker. (this can be quite a stretch).
The user has engaged with the site multiple times (right now, twice within at least 5 minutes).
The user has not already added your site to the home-screen.
So, in summary it is complex and full of false positives and false negatives. However if all you want to do is detect if you should display a banner to prompt the user to add your web-app to the homescreen then Chrome already has a solution for you.
We also have a full range of samples on our samples site.
I think you can do it. Simply add query string to start_url in manifest.json and in your javascript check if start url is having that query string. If query string is found then yeah app is installed.
first you get the list of apps on the device
List<ApplicationInfo> packs = pm.getInstalledApplications(0);
then you use getLaunchIntentForPackage()
Now that you've got the list of packages installed on your device,
iterate through them and call getLaunchIntentForPackage() on each
item.
If a valid intent is returned, it exists on the Launcher, else if null
is returned, the package does not launch from the Launcher screen.
Note that Home screen shortcuts are a subset of the Launcher apps.