Search code examples
javaandroidandroid-activityandroid-viewrajawali

Android switching between gl views causes app freeze


I'm still learning how to program in Android and I created an Activity with a start view:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.my_activity);

        mMainView = (RajawaliSurfaceView) findViewById(R.id.my_view);
        mMainView.setSurfaceRenderer(new Renderer());
        mMainView.setZOrderOnTop(false);

        // Also set up a surface view I'd like to switch on back and forth
        mGLView = new GLSurfaceView(this);
        mGLView.setEGLContextClientVersion(2);
        mGLView.setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
        mGLView.setRenderer(cameraRenderer_ = new GLRenderer(this));
        mGLView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
    }

This works and I can even switch to the GL view seamlessly:

public void switchToGLView(View view) {
  setContentView(mGLView);
}

But after I'm done with the GL thing, I have the GLRenderer call a restore function in my main activity

public void restoreRenderer() {
  // Restoring the previous view (my_view) with the non-GL renderer
  setContentView(mMainView); <<<<<< FREEZES HERE (blackscreen)
}

and unfortunately this never works: the screen remains black from the previous GL renderer and by debugging the setContentView line I discovered that:

  • I don't get ANY error or warning log in the Android Monitor

  • The execution NEVER continues. It gets stuck in there forever

Any tips or hints on how to solve this?


Solution

  • You could solve this problem using a Frame or Relative layout. If I understand correctly, both mMainView and mGLView are fullscreen. There are a few options:

    You could change the visibility of the views:

    public void switchViews() {
      if(mMainView.getVisibility() == View.VISIBLE){
         mMainView.setVisibility(View.GONE);
         mGLView.setVisibility(View.VISIBLE);
      } else{
         mMainView.setVisibility(View.VISIBLE);
         mGLView.setVisibility(View.GONE);
      }
    }
    

    The other option is to change the Z order of the views:

    public void switchViews(View view){
      view.bringToFront();
      view.invalidate();
    }
    

    Unless there are any drawbacks you encountered using the above methods.