0

I have a service (Foreground, STICKY), and an activity (LaunchMode.SingleTask) in the same app.

There are 2 mvvmcross setup scenarios:

  1. Service is started from boot, mvvmcross is initialized with the 'setup.EnsureInitialized' method
  2. Service is started from the Activity, and mvvmcross is initialized the 'classic' way

Sometimes because of low memory pressure, or unexpected exception, my service is killed or dies.

I tap then on my launcher,

10-13 01:05:06.699   497   507 I ActivityManager: START {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=LoneWorker.Client.Android.App/loneworker.client.android.app.TrialSplashScreen bnds=[240,223][360,379] u=0} from pid 11747
10-13 01:05:06.769   497  7269 I ActivityManager: Start proc LoneWorker.Client.Android.App for activity LoneWorker.Client.Android.App/loneworker.clientandroid.app.views.HomeView: pid=11980 uid=10081 gids={3003, 1015, 1023, 1028}

but then the splash screen doesn't appear like a normal app start, and the application fails right away.

10-13 01:05:07.959 11980 11980 I MonoDroid: UNHANDLED EXCEPTION:
10-13 01:05:07.969 11980 11980 I MonoDroid: System.NullReferenceException: Object reference not set to an instance of an object
10-13 01:05:07.969 11980 11980 I MonoDroid: at Cirrious.CrossCore.Mvx.Resolve<Cirrious.MvvmCross.Plugins.Messenger.IMvxMessenger> () <0x00038>
10-13 01:05:07.969 11980 11980 I MonoDroid: at LoneWorker.ClientAndroid.App.Views.HomeView..ctor () <0x0002f>
10-13 01:05:07.969 11980 11980 I MonoDroid: at (wrapper dynamic-method) object.ee0d83da-1657-4884-bf34-4fc266ad42f0 (intptr,object[]) <0x0003f>
10-13 01:05:07.969 11980 11980 I MonoDroid: at Java.Interop.TypeManager.n_Activate (intptr,intptr,intptr,intptr,intptr,intptr) <0x00283>
10-13 01:05:07.969 11980 11980 I MonoDroid: at (wrapper dynamic-method) object.2fd07179-5f93-420a-8653-1fbe1c8a2f2e (intptr,intptr,intptr,intptr,intptr,intptr) <0x0006b>

In the log I have just an exception because the messenger couldn't be resolved by the Mvx container. => It can't be resolved simply because the setup process doesn't start. It behaves like the app was already initialized...

So does the previous app singleton from the previous crashed service is still up somewhere ?

Can I force the app to reinitialize ?

I'm a bit confused by this strange behavior...

Roubachof
  • 3,351
  • 3
  • 23
  • 34

1 Answers1

1

The stack trace shows that your app is being created in a "new" process, but is being launched via HomeView.

Your code is then calling Mvx.Resolve within its constructor.

10-13 01:05:07.969 11980 11980 I MonoDroid: at Cirrious.CrossCore.Mvx.Resolve<Cirrious.MvvmCross.Plugins.Messenger.IMvxMessenger> () <0x00038>
10-13 01:05:07.969 11980 11980 I MonoDroid: at LoneWorker.ClientAndroid.App.Views.HomeView..ctor () <0x0002f>

The exception is then caused inside:

    public static object Resolve(Type serviceType)
    {
        var ioc = MvxSingleton<IMvxIoCProvider>.Instance;
        return ioc.Resolve(serviceType);
    }

This will be because MvxSingleton<IMvxIoCProvider>.Instance is null (https://github.com/MvvmCross/MvvmCross/blob/3.2/CrossCore/Cirrious.CrossCore/Mvx.cs#L36)

Because of Android's lifecycle, MvvmCross doesn't check that Setup has been done during the construction of a view - but instead during the OnCreate method.

I'd suggest you move your code out of the View constructor and into OnCreate - somewhere after the base.OnCreate call.

Note, that in these situations - if you are running Setup on the UI thread during the activity/view creation, then the UI can appear to "pause" to users.

Stuart
  • 66,722
  • 7
  • 114
  • 165
  • yup ! that was it :). There is however something strange about it: When the app is relaunched after a crash, it doesn't go through Splash screen, but directly to the last view before the crash.... – Roubachof Oct 13 '14 at 08:05