Android - 从渲染器切换活动

发布于 2024-12-05 05:50:23 字数 739 浏览 1 评论 0原文

在我的 Android 应用程序中,我想从渲染器切换活动。当我创建渲染器时,我在构造函数中传递上下文。在我的渲染器中的 onDrawFrame 函数中:

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;
    }


}

这会在我的主要活动中调用 onPause() 来处理一些 OpenGL 人员。

切换活动时出现错误:

此时我收到以下错误:

在没有当前上下文的情况下调用 OpenGL ES API(每个线程记录一次)

有人可以帮助我吗?我意识到这是因为对 OpenGL 的调用不是从 OpenGL 线程进行的,但我该如何修复它?

从渲染器内切换活动的正确方法是什么?

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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蘸点软妹酱 2024-12-12 05:50:23

我使用了这些来源:
如何从非活动启动活动活动类?
如何重新启动 Android Activity
这是在非活动类(例如渲染器)的类内切换活动的解决方案

    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);

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);

江湖正好 2024-12-12 05:50:23

来自 GLSurfaceView.Renderer 文档:

线程化
渲染器将​​在单独的线程上调用,以便渲染性能与 UI 线程解耦。客户
通常需要从 UI 线程与渲染器进行通信,
因为那是接收输入事件的地方。客户可以
使用任何标准 Java 技术进行跨线程通信
沟通,或者他们可以使用
queueEvent(Runnable)
方便的方法。

您应该能够通过从 onPause() 方法发布的此类 Runnable 正确执行 OpenGL 调用。

我不认为您从 GL 线程启动活动切换这一事实是问题的一部分(它确实看起来有点奇怪,但我假设您有充分的理由) :))。

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 :)).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文