dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.82k stars 1.66k forks source link

Android https app links (deep links) don't work if app isn't already running #22336

Open pengalo opened 1 month ago

pengalo commented 1 month ago

Description

After creating an app link, it works great if the app is already running in the emulator or on the device. However, if the app isn't already running, it starts the app, but then immediately exits without executing the app link activity.

Steps to Reproduce

Use the excellent sample from Redth called "MAUI.AppLinks.Sample" (following this is what finally got anything working at all for my app links). Run the application against an Android emulator or physical device (release or debug build). Use the suggested adb command to test: adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://redth.dev/items/1234" Observe that the app pops up an alert showing the URL that it received Close the application in the simulator or on the device Run the same adb command again Observe that the app will flash the home page but then immediately exit

The exact same thing is happening with my application too. There doesn't seem to be any useful information in the Android Device Log and I've put try/catch and Debug.WriteLine files all over in my code and it just seems to die without any error listed.

Link to public reproduction project repository

https://github.com/Redth/MAUI.AppLinks.Sample

Version with bug

8.0.7 SR2

Is this a regression from previous behavior?

Not sure, did not test other versions

Last version that worked well

Unknown/Other

Affected platforms

Android

Affected platform versions

Android 13, Android 14

Did you find any workaround?

No

Relevant log output

No response

github-actions[bot] commented 1 month ago

Hi I'm an AI powered bot that finds similar issues based off the issue title.

Please view the issues below to see if they solve your problem, and if the issue describes your problem please consider closing this one and thumbs upping the other issue to help us prioritize it. Thank you!

Open similar issues:

Closed similar issues:

Note: You can give me feedback by thumbs upping or thumbs downing this comment.

PureWeen commented 1 month ago

Can you test with the latest nightly build? https://github.com/dotnet/maui/wiki/Nightly-Builds

The latest nightly build should include a better exception about how you can resolve your scenario

pengalo commented 1 month ago

Ok, I tried with the nightly build (8.0.40-nightly.10612), with the same behavior. Did not fix the issue of deep linking when the app isn't already running.

One note is that the adb command to use is actually: adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://redth.dev/items/1234" dev.redth.applinkssample

The previous adb command I listed (without the "dev.redth.applinkssample" at the end) actually causes the entire Android emulator to crash whether the app is running or not.

PureWeen commented 1 month ago

Ok, I tried with the nightly build (8.0.40-nightly.10612), with the same behavior. Did not fix the issue of deep linking when the app isn't already running.

One note is that the adb command to use is actually: adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "https://redth.dev/items/1234" dev.redth.applinkssample

The previous adb command I listed (without the "dev.redth.applinkssample" at the end) actually causes the entire Android emulator to crash whether the app is running or not.

Can you list out the entire exception here?

pengalo commented 1 month ago

The adb command completes without an error, and because the app is started by running adb, not by VS, there is no exception caught or logged. I don't see any errors or anything that seems useful in the Device Log.

Zhanglirong-Winnie commented 1 month ago

Verified this issue with Visual Studio 17.10 Preview 7.0 (8.0.21&8.0.7). Can repro on android platform with sample project. image

pengalo commented 1 month ago

I may have found a workaround. I don’t think it’s a permanent fix and I can’t be sure there aren’t unexpected side-effects, but it seems to be working for now in my project. Basically, when the app isn’t already running, it seems to get started in a state without a MainActivity, which seems to cause the problems somewhere deep in MAUI code. For the workaround, I detect if there is already a MainActivity, and if not, I start it using “Platform.CurrentActivity?.StartActivity(typeof(MainActivity))”. However, this causes the Windows array to be in a different order, so you need to use a different index to do any UI functions (such as DisplayAlert in the sample code). I haven’t figured out a way to tell which window is the correct one to use other than if the app was already running, use index [0] and if it started to handle to app link, then it is the last window in the array (in the sample, that is index[1]).

pengalo commented 1 month ago

In further testing, the workaround isn't consistent. I tried adding a 250 ms delay after starting MainActivity and that helps, but it still sometimes doesn't display the deep link content (doesn't show the DisplayAlert). I'm hesitant to add a longer delay so I'm looking for another workaround.