1

I am using MAUI under .NET 7 and I have a launcher application set as default in Android simulator. The app crashes when it is restarted, but works fine otherwise. The application restarts after closing it, because it is set as the default launcher. I get the following exception:

System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Google.Android.Material.ImageView.ShapeableImageView'.' 

What is the problem and how do I fix it?

3 Answers 3

1

There is no real problem with MAUI DI, but there is a problem with Shell in MAUI. The Shell is using an internal cache. So even when you declare your View or ViewModel as Transient, they are cached by the Shell and the preceding instance is returned, not a fresh new one, annihilating the effect of Transient service registering. But as they are not registered as Singleton, there not real Singleton and can be collected depending on the state of the Shell cache. It's an old problem that Microsoft does not seem to correct (a simple bool property in the shell "UseCache" true/false will solve this problem). For some App, putting all views and Viewmodels in Singleton mode car be ok, but in a lot of other cases this is not a good solution. The only way to get things working correctly (Views and ViewModels registered as Transient are really Transient) is to banish the Shell and use NavigationPage instead. So, if you're using the Shell, even if you declare your View and ViewModel as Transient you will still have problems.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you provide any GitHub issues regarding this?
0

The MAUI project I am working on is using Dependency Injection and the problem got away after I changed all View dependencies from Singleton to Transient. I had this idea after reading this answer from another post that mentions problems with DI in MAUI.

I didn't find a clear article stating you should define Views as Transient. In my case, the app worked fine with Singleton as long as it was not restarted. I believe it might be a corner case where Android resources are cleared but the View in C# is still there.

2 Comments

could you please explain how do you define that MAUI application will be restarted - " set as the default launcher"?
It's just configured as Android Launcher and when it automatically restarts when I close it from, but it crashes right after restart. I need this for updating my app in background.
-1

.Net MAUI Android: Cannot access a disposed object 'System.Net.Http.HttpClient', when calling an API again, for first time it works fine. . My Old code:

public class NetworkClient { private static readonly Lazy<HttpClient> _clientInstance = new Lazy<HttpClient>(() => { HttpClient client = new HttpClient(); client.BaseAddress = new Uri(Constants.BASE_URL); string jwtToken = new SessionManager().getToken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); return client; }); private NetworkClient() { } public static HttpClient GetClient() { return _clientInstance.Value; } } 

Working Solution (Creating new Instance every time, As it's not a best practice but had no other solution for now.):

public class NetworkClient { /* Method to get the HttpClient instance , Everytime new to avoid: System.ObjectDisposedException: 'Cannot access a disposed object */ public static HttpClient GetClient() { HttpClientHandler handler = new HttpClientHandler(); HttpClient client = new HttpClient(handler, false); client.BaseAddress = new Uri(Constants.BASE_URL); string jwtToken = new SessionManager().getToken(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", jwtToken); return client; } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.