You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When trying to initialize MvvmCross from a background worker when the main application is not open, calling MvvmCross.Core.MvxSetupSingleton.EnsureInitialized() crashes the app with System.InvalidOperationException: Cannot create Views Container without ApplicationContext due to public Context? ApplicationContext => _currentTopActivity?.Activity?.ApplicationContext; being null in MvvmCross.Platforms.Android.Core.MvxAndroidSetup
MvvmCross should initialize without a current top activity
Reproduction steps
Create a periodic work request in MainApplication that will run after the app is closed
public class MainApplication : MvxAndroidApplication<Setup, App>
{
public MainApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer)
{
}
public override void OnCreate()
{
base.OnCreate();
var constraintsBuilder = new Constraints.Builder();
constraintsBuilder.SetRequiredNetworkType(NetworkType.NotRequired);
var constraints = constraintsBuilder.Build();
var workRequest = new PeriodicWorkRequest.Builder(typeof(DummyWorker), new TimeSpan(0, 1, 0))
.SetConstraints(constraints)
.Build();
WorkManager.GetInstance(ApplicationContext).EnqueueUniquePeriodicWork(nameof(DummyWorker), ExistingPeriodicWorkPolicy.Replace, workRequest);
}
}
public class DummyWorker : ListenableWorker, CallbackToFutureAdapter.IResolver
{
public DummyWorker(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
Initialize();
}
protected DummyWorker(Context context, WorkerParameters workerParams) : base(context, workerParams)
{
Initialize();
}
private void Initialize()
{
var setup = MvxAndroidSetupSingleton.EnsureSingletonAvailable(ApplicationContext);
setup.EnsureInitialized();
}
public override IListenableFuture StartWork()
{
return CallbackToFutureAdapter.GetFuture(this);
}
public Java.Lang.Object? AttachCompleter(CallbackToFutureAdapter.Completer completer)
{
completer.Set(Result.InvokeSuccess());
return string.Empty;
}
}
Configuration
Version: 9.0.8
net7.0-android
Platform:
馃 Android
The text was updated successfully, but these errors were encountered:
This should potentially be fixed when #4546 gets merged, then you don't need to call EnsureInitialized all over the place.
But I guess also I would need to change MvxAndroidSetupSingleton to not get ApplicationContext from top Activity. Will prepare a PR for this.
In the meantime you can override InitializeViewsContainer and provide your own CreateViewsContainer method to use instead, which uses ApplicationContext which is not from top Activity
馃悰 Bug Report
When trying to initialize MvvmCross from a background worker when the main application is not open, calling
MvvmCross.Core.MvxSetupSingleton.EnsureInitialized()
crashes the app withSystem.InvalidOperationException: Cannot create Views Container without ApplicationContext
due topublic Context? ApplicationContext => _currentTopActivity?.Activity?.ApplicationContext;
being null inMvvmCross.Platforms.Android.Core.MvxAndroidSetup
Expected behavior
MvvmCross should initialize without a current top activity
Reproduction steps
Create a periodic work request in MainApplication that will run after the app is closed
Configuration
Version: 9.0.8
net7.0-android
Platform:
The text was updated successfully, but these errors were encountered: