Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:16:39+00:00 2026-06-14T09:16:39+00:00

I’m trying to dispose and restart an OpenGLView or AndroidGameView within the same Activity,

  • 0

I’m trying to dispose and restart an OpenGLView or AndroidGameView within the same Activity, but it seems the game can’t start another time after disposed inside the same Activity. Here is my test using monodroid game sample project:

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();
    //}

Thanks in advance for your help.

Update with my new code to avoid reuse 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");

                }
            });
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:16:41+00:00Added an answer on June 14, 2026 at 9:16 am

    I had a play around with your code and found that the Timer was still running but the block inside RunOnUiThread() wasn’t being called. I removed the view.Stop() method and the view.Dispose() and it started working correctly.

    Here is my full code (using a 5000ms interval for the time)

        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());
                }
            });
        }
    

    The old View appears to still be garbage-collected (see in the log below). I don’t think the extra calls were necessary. I ran the application for about 15 minutes straight with no issues.

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Specifically, suppose I start with the string string =hello \'i am \' me And
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.