0

I'm trying to use Deep Link for Android in Maui, but when I open my application from the link newapp://result, it opens either the home page or the last page that was open in my application. And when I try to navigate to another page, the application crashes. This happens whether I use appshell navigation or navigate using Push. This is how my activity is configured: [IntentFilter(new[] { Intent.ActionView },

          Categories = new[]
            {
                Intent.ActionView,
                Intent.CategoryDefault,
                Intent.CategoryBrowsable,
                
            },
          DataScheme = "newapp"
        )
    ]
    [Activity(Exported = true, Theme = "@style/Maui.SplashTheme")]
    internal class CallbackActivity: MauiAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            
            base.OnCreate(savedInstanceState);
            var data = Intent.DataString;


        }
       
    }
.

UPDATE In this repository I created an example of the problem, to test it you must run it on an android device, then go to the demoapp://result page and the application should open, there press the navigate button and the application breaks, but if you use the application normally without opening it by demoapp://result, it works without any problem. https://github.com/bikotoru/problem-intent-maui/tree/main

  • What exception causes the crash? What do the logs show? – Jason May 19 '23 at 18:49
  • [0:] Microsoft.Maui.Controls.Shell: Warning: Failed to Navigate Back System.ArgumentNullException: Value cannot be null. (Parameter 'key') – victor ureta May 19 '23 at 19:40
  • Jason, include a example in a repo with the problem – victor ureta May 19 '23 at 20:02
  • `And when I try to navigate to another page, the application crashes. ` How can we reproduce this problem? Could you please post the steps of reproducing this problem? I downloaded your demo and tried to deploy it to my android emulator. There are two buttons on the first page, and it works properly on my side. – Jessie Zhang -MSFT May 22 '23 at 02:37
  • @JessieZhang-MSFT Steps to Reproduce Open application normally 2.- Test side navigation 3.- Test navigation using the Navigate button 4.- Everything works perfect Problem: 1.- Open application using link demoapp://result 2.- Touch the Navigate button 3.- The error occurs – victor ureta May 22 '23 at 04:00

1 Answers1

0

Yes, it is just the case as you said.

I notice there are two acticities (MainActivity and CallbackActivity) on platform android of your app you shared.

If I add the following code to CallbackActivity,this question will arise.

    [Activity(Label = "SecondApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "myapp" }
    )
]

But if I remove above code for CallbackActivity and copy it to MainActivity,the problem will disappear.

As a test,I changed the package name of your app to com.xamarin.secondapp and added the following code to MainActivity:

    [Activity(Label = "SecondApp", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true,Exported =true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] 
    [
    IntentFilter
    (
        new[] { Android.Content.Intent.ActionView },
        Categories = new[]
            {
                Android.Content.Intent.CategoryDefault,
                Android.Content.Intent.CategoryBrowsable
            },
        DataSchemes = new[] { "myapp" }
    )
]
    public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);
            LoadApplication(new App());
        }
    }

And on another app which will open your app(com.xamarin.secondapp), I added queries tag in file manifest.xml.

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.openappapp1">
    <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33" />
    <application android:label="OpenappApp1.Android" android:theme="@style/MainTheme">

      </application>
      <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


      <queries>
            <package android:name="com.xamarin.secondapp" />
      </queries>
</manifest>

Also,I added a button on MainPage.xaml.cs which will navigate to your app(com.xamarin.secondapp):

public async void OpenSecondApp()
{

    var supportsUri = await Launcher.Default.CanOpenAsync("myapp://");
    if (supportsUri)
        await Launcher.Default.OpenAsync("myapp://com.xamarin.secondapp");

}

The above code works fine and does not have the problem you mentioned.

Jessie Zhang -MSFT
  • 9,830
  • 1
  • 7
  • 19