I trying to port app from WinCE to Xamarin.Android. This app is using states machine to switch between states (by user clicking or particular background processing). Every state has own view.
So, main idea is to keep in some provider all available views as List and show one of them by state switching. It might be activated from both UI thread or Worker thread. As the following:
public List<IView> Views { get; protected set; } public virtual void Init() { Views.Add(new MasterView()); Views.Add(new LoginView()); Views.Add(new MainMenuView()); } public T GetView<T>() where T : InView { return (T)Views.FirstOrDefault(view => view is T); }
Every activity is inherited from base activity class.
[Activity(Label = "MasterView")] public class MasterView : BaseView, IMasterView { protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here } } public class BaseView : Activity, IView //is in class library project { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); SetContentView(Resource.Layout.baseview); } }
Now, when Init method starts to add new MasterView i'm getting error "Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"
All this stuff works like a charm under WinCE.
Will it work at Xamarin.Android? How to modify it?
"Java.Lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()"
This exception usually exception when you are dealing with the UI in a worker thread. You should do it in the main thread.
Please refer to the following links for more information:
https://docs.microsoft.com/en-us/xamarin/essentials/main-thread?context=xamarin/android
https://developer.android.com/training/multiple-threads/communicate-ui
Answers
Any ideas?
@DenisMishin
This exception usually exception when you are dealing with the UI in a worker thread. You should do it in the main thread.
Please refer to the following links for more information:
https://docs.microsoft.com/en-us/xamarin/essentials/main-thread?context=xamarin/android
https://developer.android.com/training/multiple-threads/communicate-ui
@BillyLiu
Yes, i understand that. Unfortunatelly i had to refuse idea to use another threads and use UI thread to initialize view.
@BillyLiu
Sorry but i stiil don't get answer: Is it possible to have a List of available activities to get one of them from this list using fabric?
And how to start this activity if it already instanciated? Or it is forbidden?