开发者

Android - Switch activities from renderer

开发者 https://www.devze.com 2023-04-05 21:50 出处:网络
In my Android app I want to switch activities from my renderer. When I create the Renderer I pass context in the constructor. In my Renderer in the onDrawFrame function:

In my Android app I want to switch activities from my renderer. When I create the Renderer I pass context in the constructor. In my Renderer in the onDrawFrame function:

public MyRenderer(Context ctx){

    this.context=ctx;
}

public void onDrawFrame(GL10 gl) {

    testFlag = renderFrame();


    if(testFlag > 0)
    {
        Intent myIntent = new Intent(this.context, MyActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        this.context.startActivity(myIntent);
        testFlag = 0;
        return;
    }


}

This calls the onPause() in my main activity which handles some OpenGL staff..

What I get is an error when switching the acivities开发者_StackOverflow中文版:

At that point I receive the following error:

call to OpenGL ES API with no current context ( logged once per thread)

Can anyone please help me? I realize that this is caused because a call to OpenGL is not made from the OpenGL thread but how do I fix it??

What is the proper way to switch activities from within the renderer?


I used those sources:
How can I start an Activity from a non-Activity class?
How do I restart an Android Activity
This is solution to switch activities inside class that is non-Activity class (for example Renderer)

    private Context context; 
    public MyGLRenderer(Context context)  {
    super();
    this.context = context;                         
}

            Intent myIntent = new Intent(context, MyActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            ((Activity)context).finish();
            ((Activity)context).overridePendingTransition(0, 0);
            context.startActivity(myIntent);
            ((Activity)context).overridePendingTransition(0, 0);


From the GLSurfaceView.Renderer docs:

Threading
The renderer will be called on a separate thread, so that rendering performance is decoupled from the UI thread. Clients typically need to communicate with the renderer from the UI thread, because that's where input events are received. Clients can communicate using any of the standard Java techniques for cross-thread communication, or they can use the queueEvent(Runnable) convenience method.

You should be able to do your OpenGL calls correctly through such a Runnable, posted from your onPause() method.

I don't think the fact that you're initiating the activity switch from the GL thread is part of the issue (it does seem a bit weird, but I will assume you have a good reason for it :)).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号