我正在尝试在同一个 Activity 中处理和重新启动 OpenGLView 或 AndroidGameView,但在同一个 Activity 中处理后似乎游戏无法再次启动。这是我使用 monodroid 游戏示例项目的测试:
GLView1 view;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create our OpenGL view, and display it
//view = new GLView1(this);
//SetContentView(view);
Timer timer = new Timer(OnTimerDone, this, 3000, 3000);
}
void OnTimerDone(object state)
{
System.Diagnostics.Debug.WriteLine("timer");
((Activity)state).RunOnUiThread(() =>
{
if (view != null)
{
//view.Stop();
view.Dispose();
view = null;
SetContentView(null);
GC.Collect();
}
else
{
view = new GLView1((Activity)state);
//view.Resume();
SetContentView(view);
}
});
}
//protected override void OnPause()
//{
// base.OnPause();
// view.Pause();
//}
//protected override void OnResume()
//{
// base.OnResume();
// view.Resume();
//}
在此先感谢您的帮助。
更新我的新代码以避免重用 SetContentView:
GLView1 view;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Create our OpenGL view, and display it
//view = new GLView1(this);
//SetContentView(view);
SetContentView(Resource.Layout.Main);
Timer timer = new Timer(OnTimerDone, this, 3000, 3000);
}
void OnTimerDone(object state)
{
((Activity)state).RunOnUiThread(() =>
{
LinearLayout linearLayoutMain = ((Activity)state).FindViewById<LinearLayout>(Resource.Id.linearLayoutMain);
if (view != null)
{
System.Diagnostics.Debug.WriteLine("timer delete");
linearLayoutMain.RemoveView(view);
try
{
view.Stop();
view.Dispose();
view = null;
//SetContentView(null);
GC.Collect();
}
catch (Exception ex)
{
//Android.Util.Log.Debug("ex:", ex.ToString());
System.Diagnostics.Debug.WriteLine("ex:" + ex);
}
}
else
{
view = new GLView1((Activity)state);
view.Run();
//view.Resume();
//SetContentView(view);
linearLayoutMain.AddView(view);
System.Diagnostics.Debug.WriteLine("timer create");
}
});
}