xamarin.android - MvvmCross - Android: How to perform App initialization on Main Thread -
i have android application using mvvmcross. app launches via mvxsplashscreenactivity main launcher , provided setup class deriving mvxandroidsetup.
it seems mvxandroidsetup.createapp() override called on threadpool thread (see https://github.com/slodge/mvvmcross/blob/vnext/cirrious/cirrious.mvvmcross.droid/views/mvxbasesplashscreenactivity.cs#l79).
what best way ensure parts of app initialization performed on mainthread?
most of modern platforms - windowsstore, windowsphone , ios - allow bundle static default.jpg (or similar) entertain users while app starts.
android doesn't - launches app using activity marked mainlauncher.
if app needs initialization work (as mvvmcross apps do) leaves choice - do work on ui thread (which results in unresponsive ui) or display placeholder splashscreen , initialization work on background thread.
this mvvmcross tries allow
- it provides splashscreen class
- it overrides
oncreateof splashscreen minimal work can on ui thread during splashscreenoncreate(during time ui black horrible) - then bulk of init on threadpool thread - @cheesebaron has pointed out on mvxbasesplashscreenactivity.cs#l79
the bulk of initialisation - loading types, starting services, recovering settings, loading language files, etc - shouldn't need done on ui thread.
if there part of initialization need on ui thread, it's app work out how , when marshal work ui - e.g.
- by using mvxmainthreaddispatcher.cs
- by deferring task until later time (e.g. using lazy ioc triggered viewmodel creation)
- by overriding splashscreen's oninitialisationcomplete method
however, @ no point should try marshal long-lasting work onto ui thread... ui thread ui, not heavy calculation or blocking work.
you should aim keep ui responsive, during startup.
detailed note:
the above description covers 'normal launch' of app e.g. android home page.
however, if dive deeper, not way app can started - can started push notifications, recovering being killed ("tombstoned" in wp speak) or things broadcast receivers.
in situations, mvvmcross app initialisation may occur in other ways described above:
- in case of mvxactivity being directly launched (e.g push or after recovering tombstoning), whole of setup run on ui thread during oncreate of activity - isn't ideal , hope framework can improve on in future.
- in case of background service starting, it's app engineer work out setup when - e.g. see using mvvmcross content providers , activities
one opportunity alternative startup route subclass android application object - see http://developer.android.com/reference/android/app/application.html
Comments
Post a Comment