Xamarin.Forms universal linking to Android always re-creates the app.

Solomon Fried 41 Reputation points
2021-06-30T19:01:03.107+00:00

I am working on having my App support universal Linking.
I have it working on UWP and am moving on to Android.

The linking seems to work fine and I can start my app running on Android using a link using the command
adb shell am start -a android.intent.action.VIEW -c android.intent.category.BROWSABLE -d "http://mydomain.com.com/xxx

My problem is that if the app is already running and the link is used, the the running app is restarted from the begining.
In other words, OnCreate is called again on a new instance of MainActivity and a new MainPage is created on the call to new App()

Interestingly, the OnNewIntent override is never called. Only the OnAppLinkRequestReceived in the shared project App.xaml.cs. and it receives the Url.

This is an issue because a user hitting a link for the app will lose their state.

Any help would be appreciated

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,325 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 72,251 Reputation points Microsoft Vendor
    2021-07-01T03:14:51.08+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Please add LaunchMode =LaunchMode.SingleTask, in your Activity tag like following screenshot.

    110800-image.png

    Basically what this does is create a new task and a new instance will be pushed to the task as the root one. However, if any activity instance exists in any tasks, the system routes the intent to that activity instance through the onNewIntent() method call. In this mode, activity instances can be pushed to the same task. And if the user clicks the BACK key from the current activity, the system will return the user to the previous activity.

    For more details, you can refer to this thread.
    https://stackoverflow.com/a/47452460/10627299

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Solomon Fried 41 Reputation points
    2021-07-01T13:05:03.413+00:00

    Thanks to @Leon Lu (Shanghai Wicresoft Co,.Ltd.)

    Should be noted that the default processing for OnNewIntent is to call SendOnAppLinkRequestReceived with the intent URI. This, in turn will call OnAppLinkRequestReceived in the Shared Code project.

    Many examples I have seen regarding universal link, had code like in MainActivity.cs

     protected override void OnNewIntent(Intent intent) {  
        // perform any uri processing here  
        _app.SendOnAppLinkRequestReceived(new Uri(intent.Data);  
        base.OnNewIntent(intent);  
    }  
    

    This is unnecessary since _app.SendOnAppLinkRequestReceived(new Uri(intent.Data); will call your OnAppLinkRequestReceived which is what the base OnNewIntent will do anyway. Calling base.OnNewIntent actually calls OnAppLinkRequestReceived again.

    Any code to parse and take action on the uri is better off done in the Shared code which will handle linking from all platforms.

    0 comments No comments