我一直在开发基于C#Xamarin的android应用程序(您可以在Java中安装遮阳篷,我不在乎),并且基于之前所做的工作,我不得不创建一个类来为应用程序,以便公共代码可以调用该类在“活动”之间导航。由于我的应用程序的性能问题,我当前的解决方案是错误的。由于不允许显示真实的代码,因此我举一个例子:
class NavigationService : INavigationService
{
public AppCompatActivity CurrentContext { get; set; }
public AppCompatActivity PreviousContext { get; set; }
public ViewFromFramework PreviousView { get; set; }
public NavigationService (AppCompatActivity context)
{
CurrentContext = context;
PreviousContext = null;
}
public void Navigate(ViewFromFramework currentView, ViewFromFramework destinationView)
{
PreviousView = currentView;
switch (destinationView)
{
case ViewFromFramework .Splashscreen:
{
this.NavigateTo<SplashScreenActivity>();
break;
}
case ViewFromFramework .Login:
{
this.NavigateTo<LoginActivity>();
break;
}
default:
break;
}
}
private void NavigateTo<T>()
{
PreviousContext = CurrentContext;
Intent nextActivity = new Intent(this.CurrentContext,typeof(T));
this.CurrentContext.StartActivity(nextActivity);
PreviousContext.Finish();
}
}
继承的类称为INavigationService:
public enum ViewFromFramework
{
Splashscreen,
Login
}
public interface INavigationService
{
void Navigate(ViewFromFramework currentView, ViewFromFramework destinationView);
ViewFromFramework PreviousView { get; set; }
}
在每个活动中,我都在on create内:
NavigationService.CurrentContext = this;
由于某些活动运行AsyncTasks,所以当我完成活动时,我得到 错误:
View = com.android.internal.policy.impl.PhoneWindow $ DecorView {1306f5f1 G.E ..... R ..... ID 0,0-257,133}未附加到窗口管理器
有更好的方法吗?