Skip to content

Instantly share code, notes, and snippets.

@alexshikov
Last active May 23, 2018 13:43
Show Gist options
  • Save alexshikov/ff8b522f7e8b55ec719f68e1765b0314 to your computer and use it in GitHub Desktop.
Save alexshikov/ff8b522f7e8b55ec719f68e1765b0314 to your computer and use it in GitHub Desktop.
Fix to NavigationPageRenderer constructor crash
using System;
using Android.Runtime;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android.AppCompat;
[assembly: ExportRenderer (typeof (NavigationPage), typeof (MyApp.Droid.Renderers.FixedNavigationRenderer))]
namespace MyApp.Droid.Renderers
{
// WARNING
// There is a crash happens for unknown reason:
// System.MissingMethodException: No constructor found for
// Xamarin.Forms.Platform.Android.AppCompat.NavigationPageRenderer::.ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
// see https://bugzilla.xamarin.com/show_bug.cgi?id=40258
// --
// As a solution we override NavigationPageRenderer to provide the constructor.
// Although then there is a NullReferenceException happens in
// OnDetachedFromWindow() method which we are fixing too.
public class FixedNavigationRenderer: NavigationPageRenderer
{
public FixedNavigationRenderer()
: base ()
{}
public FixedNavigationRenderer (IntPtr javaReference, JniHandleOwnership transfer)
: base ()
{
}
protected override void OnDetachedFromWindow ()
{
if (Element == null)
{
return;
}
base.OnDetachedFromWindow ();
}
}
}
@hiraldesai
Copy link

This is excellent - thank you!

@midix
Copy link

midix commented Jun 28, 2017

After I fixed this one, I started getting the same exceptions about other renderers - ListView, Page etc. It seems the core of the problem is here: https://developer.xamarin.com/guides/android/under_the_hood/architecture/#Premature_Dispose_Calls

And, unfortunately, Xamarin.Forms itself is not able to deal correctly with the situation when UI elements get created and disposed fast during navigation between views.

Strangely, the problem manifested itself only on older Android devices (4.x). I guess, I'll have to invent some other kinds of protection to prevent user from navigating too fast (which seems impossible task because it's not clear what is "too fast"...)

@lassana
Copy link

lassana commented May 23, 2018

@midix have you managed to minimize total occurrences of such crashes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment