You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET Maui Android深度链接问题:跳转页面时应用崩溃

.NET Maui Android深度链接导航崩溃解决方案

问题根源

你新建的CallbackActivity是独立于Maui主Activity的入口,通过它打开应用时,Maui的导航上下文未正确初始化,导致后续调用AppShell或Push导航时直接崩溃;同时单独Activity启动时默认只会打开首页/上次页面,无法触发自定义导航逻辑。

修复步骤

  1. 移除独立的CallbackActivity,将深度链接的IntentFilter直接添加到Maui的主Activity上,确保导航上下文正确初始化:
[IntentFilter(new[] { Intent.ActionView },
          Categories = new[]
            {
                Intent.CategoryDefault,
                Intent.CategoryBrowsable,
            },
          DataScheme = "newapp"
        )]
[Activity(Exported = true, Theme = "@style/Maui.SplashTheme", MainLauncher = true)]
public class MainActivity : MauiAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        HandleDeepLinkIntent(Intent);
    }

    protected override void OnNewIntent(Intent intent)
    {
        base.OnNewIntent(intent);
        HandleDeepLinkIntent(intent);
    }

    private void HandleDeepLinkIntent(Intent intent)
    {
        var deepLinkUrl = intent.DataString;
        if (!string.IsNullOrWhiteSpace(deepLinkUrl))
        {
            // 切换到主线程执行导航操作
            MainThread.BeginInvokeOnMainThread(async () =>
            {
                // 根据链接路径跳转到对应页面,示例为ResultPage
                await Shell.Current.GoToAsync("//ResultPage");
            });
        }
    }
}
  1. 关键注意事项:
  • 不要重复在Categories中添加Intent.ActionView,仅保留CategoryDefault和CategoryBrowsable即可满足深度链接要求
  • 实现OnNewIntent方法,处理应用后台运行时收到的深度链接Intent
  • 导航操作必须包裹在MainThread.BeginInvokeOnMainThread中,确保在UI线程执行

验证效果

重新部署应用后,通过newapp://result打开应用会直接跳转到指定页面,后续点击导航按钮不会再出现崩溃问题。

内容的提问来源于stack exchange,提问作者victor ureta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 03:32:53