1

我正在尝试在同一个 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");

                }
            });
    }
4

1 回答 1

1

我玩了一下你的代码,发现它Timer 仍在运行,但里面的块RunOnUiThread()没有被调用。我删除了该view.Stop()方法,view.Dispose()它开始正常工作。

这是我的完整代码(使用 5000 毫秒的时间间隔)

    GLView1 view;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        SetContentView(Resource.Layout.Main);
        Timer timer = new Timer(OnTimerDone, this, 5000, 5000);
    }

    void OnTimerDone(object state)
    {
        RunOnUiThread(() =>
        {
            try
            {
                LinearLayout linearLayoutMain = ((Activity)state).FindViewById<LinearLayout>(Resource.Id.linearLayoutMain);

                if (view != null)
                {
                    linearLayoutMain.RemoveView(view);
                    view = null;
                }
                else
                {
                    view = new GLView1(this);
                    view.Run();
                    linearLayoutMain.AddView(view);
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
        });
    }

旧视图似乎仍然被垃圾收集(见下面的日志)。我不认为额外的电话是必要的。我连续运行了大约 15 分钟的应用程序,没有任何问题。

11-16 00:30:05.828 D/dalvikvm( 2144): GC_EXPLICIT freed 119K, 6% free 8813K/9351K, paused 3ms+5ms, total 56ms
11-16 00:30:08.047 D/dalvikvm( 2144): GC_CONCURRENT freed 992K, 12% free 8270K/9351K, paused 4ms+36ms, total 162ms
11-16 00:30:10.667 D/dalvikvm( 2144): GC_CONCURRENT freed 202K, 10% free 8467K/9351K, paused 5ms+63ms, total 116ms
于 2012-11-16T00:39:41.480 回答