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>