Android: disable ES switch context context when device rotates

I have an Android app for Android.

when the device is rotated from portrait to landscape, and the back context gl is destroyed and recreated.

Is there any way to avoid this? those. always stay in a portrait or landscape?

edit: I already have this code in my activity:

@Override protected void onResume() { super.onResume(); mGLSurfaceView.onResume(); } @Override protected void onPause() { super.onPause(); mGLSurfaceView.onPause(); } 
+2
source share
3 answers

Unfortunately, until API level 11 (3.0) GLSurfaceView will always destroy the GL context. For 11 and above, you can setPreserveEGLContextOnPause (boolean preserveOnPause) .

There are ways around this by changing the source of GLSurfaceView, but any problems you encounter will be much harder to get help from others.

+4
source

If you want your GL context to be safe without breaking, you need to redefine the functions of the Activity OnPause () and OnResume () class by calling GLSurfaceView (). OnPause and GLSurfaceView (). Resume ().

 @Override protected void onPause() { super.onPause(); GLSurfaceView_Class.OnPause(); } 

// same for onResume too.

If you want to limit your application to both the other world and the landscape, you can define this in your manifest file.

android: screenOrientation = "landscape" in your activity tag.

I hope this helps

+1
source

You can save your GlContext when your application rotates so that it is not destroyed.

Instead of rewriting the entire GlSurfaceView, you can simply provide an EGLContextFactory to change how GlContext is created / destroyed.

 public class ConfigChangesGlSurfaceView extends GLSurfaceView { private static final int EGL_CONTEXT_CLIENT_VERSION_VALUE = 2; private static EGLContext retainedGlContext = null; private boolean changingConfigurations = false; public ConfigChangesGlSurfaceView(Context context) { super(context); init(); } public ConfigChangesGlSurfaceView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { changingConfigurations = false; setEGLContextClientVersion(EGL_CONTEXT_CLIENT_VERSION_VALUE); setEGLContextFactory(new GLSurfaceView.EGLContextFactory() { private final int EGL_CONTEXT_CLIENT_VERSION = 0x3098; public EGLContext createContext(EGL10 egl, EGLDisplay display, EGLConfig config) { if (retainedGlContext != null) { // Return retained context final EGLContext eglContext = retainedGlContext; retainedGlContext = null; return eglContext; } int[] attrib_list = {EGL_CONTEXT_CLIENT_VERSION, EGL_CONTEXT_CLIENT_VERSION_VALUE, EGL10.EGL_NONE}; return egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, attrib_list); } public void destroyContext(EGL10 egl, EGLDisplay display, EGLContext context) { if (changingConfigurations) { // Don't destroy and retain retainedGlContext = context; return; } if (!egl.eglDestroyContext(display, context)) { throw new RuntimeException("eglDestroyContext failed: error " + egl.eglGetError()); } } }); } @Override public void onPause() { changingConfigurations = getActivity().isChangingConfigurations(); super.onPause(); } private Activity getActivity() { Context context = getContext(); while (!(context instanceof Activity) && context instanceof ContextWrapper) { context = ((ContextWrapper) context).getBaseContext(); } if (context instanceof Activity) { return (Activity) context; } throw new IllegalStateException("Unable to find an activity: " + context); } } 
+1
source

Source: https://habr.com/ru/post/1272879/


All Articles