Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 89864

NullReferenceException when receiving android.intent.action.VIEW intent while app is foreground

$
0
0

Hi guys,

I'm trying to implement App Links in my Forms Android app using this as a reference and have run into a problem.

When my launcher activity receives the android.intent.action.VIEW intent while the app is foreground, it crashes with a NullReferenceException. After digging around a bit, I found that Application.Current is being cleared at some point, although I haven't been able to figure out when/where it is happening.

Here is the call stack when the crash occurs:

mscorlib.dll!System.Diagnostics.Debugger.Mono_UnhandledException_internal() 
mscorlib.dll!System.Diagnostics.Debugger.Mono_UnhandledException(System.NullReferenceException ex = {System.NullReferenceException: Object reference not set to an instance of an object.}) 
mscorlib.dll!object.45() 
Xamarin.Forms.Platform.Android.dll!Xamarin.Forms.Platform.Android.PlatformConfigurationExtensions.OnThisPlatform<Xamarin.Forms.Application>(Xamarin.Forms.Application element = (null)) 
Xamarin.Forms.Platform.Android.dll!Xamarin.Forms.Platform.Android.AppCompat.FragmentContainer.OnResume()
Xamarin.Android.Support.Fragment.dll!Android.Support.V4.App.Fragment.n_OnResume(System.IntPtr jnienv = 0xffffffffe76a9380, System.IntPtr native__this = 0xffffffffffdd1c2c) 
mscorlib.dll!object.45() 

Here's my launcher activity, which serves as a "splash" screen only:

[Activity(
    Label = "MyApp", 
    Icon = "@mipmap/ic_launcher", 
    Theme = "@style/Theme.LaunchScreen",
    MainLauncher = true)]

[IntentFilter(
    new [] { Intent.ActionView },
    Categories = new [] { 
        Intent.CategoryDefault,
        Intent.CategoryBrowsable
    },
    DataScheme = "https",
    DataHost = "mydomain.com",
    DataPathPrefix = "/some-path")]

public class LaunchActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        var mainIntent = new Intent(this, typeof(MainActivity));
        mainIntent.SetFlags(ActivityFlags.SingleTop | ActivityFlags.ClearTop);

        StartActivity(mainIntent);
    }
}

And here is my main activity:

[Activity(
    Theme = "@style/Theme.Main", 
    ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

public class MainActivity : Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        // Set AppCompat resources
        TabLayoutResource = Resource.Layout.Tabbar;
        ToolbarResource = Resource.Layout.Toolbar;

        // Start app
        base.OnCreate(savedInstanceState);

        Forms.Init(this, savedInstanceState);
        FormsMaps.Init(this, savedInstanceState);

        PullToRefreshLayoutRenderer.Init();

        LoadApplication(new App());
    }

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

I'm firing the intent like so:

adb -e shell am start -a android.intent.action.VIEW -d "https://mydomain.com/some-path" com.myapp

MainActivity.OnNewIntent does get called but the app crashes after that.

Something interesting worth mentioning is that if the app is launched via the VIEW intent, it does not crash if I send the intent again.

I know this will likely never happen in the wild since the intent will be fired by an external link, but I'm hesitant to put this into production as I feel like it points to some other underlying issue that's going to come back and bite me later.

Hoping someone can point me in the right direction.

Thanks.


Viewing all articles
Browse latest Browse all 89864

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>