android.netmauiintentfilterlauncher

.NET MAUI Android: How to handle when you try to launch an external app when that app is not installed?


I am using the Launcher to open a Facebook page from my app like so:

async Task OpenFacebook()
{
    try
    {
        // try to open Facebook app, if not, try to open in browser
        bool opened = await Launcher.Default.TryOpenAsync($"fb://profile?id={FacebookID}");
        if (opened)
            return;

        await Browser.OpenAsync(new Uri($"https://m.facebook.com/{FacebookID}"), BrowserLaunchMode.External);
    }
    catch (Exception ex)
    {
        
    }
}
<activity android:name="com.myapp.MainActivity" android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="fb"/>
    </intent-filter>
</activity>

This works when the Facebook app is installed, but when I uninstall the app, my intention was that it would fail over to the Browser and just open the URL. In actuality, it just crashes with this error: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.myapp/com.myapp.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.myapp.MainActivity" on path: DexPathList[[zip file..., Note: opened results in true and the function completes normally, then I get the above java exception which goes unhandled and crashes my app. I did manage to stop the crash by moving the intent filter to MainActivity.cs like so:

[IntentFilter(new[] { Intent.ActionView }, Categories = new[] { Intent.CategoryDefault, Intent.CategoryBrowsable }, DataScheme = "fb")]
public class MainActivity : MauiAppCompatActivity

But now when I press the button to open FB, opened is still true it doesn't fail over. This works as intended on iOS, but not Android. Any ideas on what I'm doing wrong?


Solution

  • I figured it out, the MAUI Launcher documentation was misleading it states here to add an intent filter for the apps you want to open. But I misunderstood deep links and intent filters, so that part is irrelevant in my case. I removed the intent filter completely and now everything works as expected.