Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Application Lifecycle and Prism Unity container "native RegisteredInstance" injection

Liger_JeromeLiger_Jerome FRUniversity ✭✭

Hi,
I'm facing an issue I don't understand :

I'm using Prism.Unity.Forms 7.0.0.396 in a .Net Standard 2.0
Everything works well... until now ;-)

I got a "UnityContainer.Resolve" NullreferenceException when my app is resuming (first launch -> ok, go background -> ok, go foreground -> crash)

I have a native "service" on Android, and I instantiate like this :

    public class AndroidInitializer : IPlatformInitializer
    {
            public AndroidInitializer(Context context) : base()
            {
                _context = context;
                localizeService = new LocalizeService();
                fb_service_app = new FB_Service_App(context); // <- this "service"
            }
            static Context _context;
            //PlatformSpecific implementation to inject
            static LocalizeService localizeService;
            static FB_Service_App fb_service_app;
            public void RegisterTypes(IContainerRegistry container)
            {
                // Register any platform specific implementations
                container.RegisterInstance<ILocalize>(localizeService);
                container.RegisterInstance<IFB_Service_App>(fb_service_app);
            }
    }

And in my VM, I just "call" the Interface by putting it on VM constructor :

public ActivityListPageViewModel(INavigationService navigationService, IFB_Service_App fb_service_app) : base(navigationService)
{
        _fb_service_app = fb_service_app;
        m_sTitle = AppResource.Menu_Item_Home;
        _iconPin = BitmapDescriptorFactory.FromStream(Ressources.Ressources.GetImageStreamFromRessources("pin.png"));
        GeoActivities = _fb_service_app.GeoActivites_Get();
        GeoActivities.CollectionChanged += GeoActivities_CollectionChanged;
        foreach (var a in GeoActivities)
            AddActivityToListAndMap(a);
}

But when the app is gone background and then foreground, Impossible to resolve this "_fb_service_app " like it was not injected (or resolved)...

Anything to manage specificaly with Lifecycle ? (android OnResume or Forms Side ?)

@BrianLagunas any idea ?

Posts

  • Liger_JeromeLiger_Jerome FRUniversity ✭✭

    Solved myself... if it can help anyone, there is my solution.

    Surprisingly, when the app go in background and get back in foreground, the "services" are not released... so they aren't null !
    My exception was raised due to my service "re-instatiation" (It's Firebase native, so he tell me that it is already instanciated)

    So the correction is simple :

    public AndroidInitializer(Context context) : base()
    {
        _context = context;
        localizeService = new LocalizeService();
        if (fb_service_app == null)
        {
            fb_service_app = new FB_Service_App(context);
        }
    }
    
Sign In or Register to comment.