0

My set-up is as follows: a TabHost has two child Activities, each with a single GLSurfaceView as content. The two Activities are of course forwarding their onPause() and onResume() events to their GLSurfaceViews.

The first Activity works as expected, however switching to the other tab has no visual impact. LogCat reveals that both onSurfaceCreated(), onSurfaceChanged() and onSurfaceDraw() are all invoked as expected on both GLSurfaceView instances.

A "fix" is to set the visibility of each GLSurfaceView using setVisibility(View.INVISIBLE/VISIBLE) in onPause() and onResume() respectively. This causes the correct views to show, but has the downside of causing a flickering effect when changing tab. This is especially evident when the GLSurfaceView does more of a job.

Essentially, how can I avoid having to set the VISIBILITY of the GLSurfaceViews?

TabActivity:

    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.mainactivity);

      TabHost tabHost = getTabHost();
      TabHost.TabSpec spec;
      Intent intent;

      // First Vortex view
      intent = new Intent().setClass(this, VortexActivity.class);
      spec = tabHost.newTabSpec("A")
            .setIndicator("A")
            .setContent(intent);
      tabHost.addTab(spec);

      // Second Vortex view
      intent = new Intent().setClass(this, VortexActivity.class);
      spec = tabHost.newTabSpec("B")
            .setIndicator("B")
            .setContent(intent);
      tabHost.addTab(spec);
}

Child activity:

    public class VortexActivity extends Activity {

        private GLSurfaceView view;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view = new GLSurfaceView(this);
        view.setRenderer(new VortexRenderer());
            setContentView(view);
        }

      @Override
      public void onResume() {
        super.onResume();
        view.onResume();
        //view.setVisibility(View.VISIBLE);
      }

      @Override
      public void onPause() {
        super.onPause();
        view.onPause();
        //view.setVisibility(View.INVISIBLE);
      }

    }

Renderer:

    public class VortexRenderer implements GLSurfaceView.Renderer {
        public void onSurfaceCreated(GL10 glArgument, EGLConfig config) {
        }
        public void onSurfaceChanged(GL10 gl, int w, int h) {
        }
        public void onDrawFrame(GL10 gl) {
            long ms = System.currentTimeMillis() % 2000;
            if(ms > 1000)
                ms = 1000 - (ms - 1000);
            float intensity = ms / 500.0f;
            gl.glClearColor(intensity, intensity, intensity, 1.0f);
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        }
    }

Layout:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:padding="0dp" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:padding="0dp" />

    <TabWidget
        android:id="@android:id/tabs"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:padding="0dp" />
</LinearLayout>

Rodskjegg
  • 489
  • 5
  • 2

1 Answers1

0

Another fix is to have a single always-visible GLSurfaceView, and have it switch the content that it is displaying.

Lawrence D'Oliveiro
  • 2,768
  • 1
  • 15
  • 13
  • How would you share the GLSurfaceView between Activities though? As far as I know you are not allowed to 'change' parent Activity on a View.(http://stackoverflow.com/questions/7813306/how-can-i-pass-image-view-between-activities-in-android) – Rodskjegg May 16 '12 at 12:53
  • Ah, fair enough. I thought they were just child layouts, not actual Activities. – Lawrence D'Oliveiro May 18 '12 at 01:38