Xamarin.Plugin.DeviceOrientation
Xamarin.Plugin.DeviceOrientation copied to clipboard
Android: Cannot access a disposed object.
DeviceOrientationImplementation.set_IsListenerEnabled (System.Boolean value) System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Plugin.DeviceOrientation.OrientationListener'.
This is thrown when the following override in the MainActivity is called after switching away from app and then back to app: public override void OnConfigurationChanged(Android.Content.Res.Configuration newConfig) { DeviceOrientationImplementation.NotifyOrientationChange(newConfig.Orientation); }
Repo steps: Tested on Android v8 and v9
- Implement override so that NotifyOrientationChange is called in Android
- Open app and use so the LockOrientation is called
- Put app in background
- Put app back in foreground
- When changing the orientation, the app will crash because the OrientationListener is disposed.
I am also experiencing this one here is our logs
Call Stack DeviceOrientationImplementation CurrentOrientation
Exception Message System.ObjectDisposedException: Cannot access a disposed object. Object name: 'Android.App.Activity'.
I think this caused when the lifecycle listener is disposed here line 24 https://github.com/jamesmontemagno/CurrentActivityPlugin/blob/f8f6e1c046087fe23117483a6f826bfe7ac627dc/src/Plugin.CurrentActivity.Android/CurrentActivity/CurrentActivityImplementation.cs. Would it be possible if the fix could have null checker here then, send the previously saved orientation back here line 55 https://github.com/wcoder/Xamarin.Plugin.DeviceOrientation/blob/12116720c8c6ee1cbf69b785a96a1253d664d0fc/src/DeviceOrientation/Plugin.DeviceOrientation.Android/DeviceOrientationImplementation.cs?
@wcoder Do you think adding the null checks in DeviceOrientationImplementation
could fix this issue?
If so: Might we introduce other side effects with this change? (e.g. broken state handling)
@wcoder @jaytilly What do you think about the null check solution?