Android的 - 从渲染线程中结束活动 [英] Android - Ending activity from within rendering thread

查看:162
本文介绍了Android的 - 从渲染线程中结束活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下午好。

我不熟悉的活动的生命周期在Android和已经读了是最好的,可能的,但我想不出如何解决在一个不错的方法,下面的。

I am unfamiliar with the activity life cycle in android and have been reading up as best as possible but I cannot figure how to solve the following in a nice manner.

我有一个GLSurfaceView活动画各种各样的东西在屏幕上。在renderering线程为该GLSurfaceView予执行所有的渲染以及实际更新逻辑(我将单独的这个出最终)

I have an activity with a GLSurfaceView to draw various things on the screen. In the renderering thread for this GLSurfaceView I perform all the rendering as well as the actual update logic (I will seperate this out eventually).

我遇到的麻烦是从我要结束活动,并使用它调用不同的生命周期方法渲染器中得出的屏幕之一。

The trouble I am having is from one of the "screens" drawn within the renderer I wish to end the Activity and have it call the various lifecycle methods.

通常情况下,我可能做到这一点与System.exit(0);然而结束在这样的活性似乎不调用的onStop();的OnDestroy();的方法。

Normally I might do this with System.exit(0); however ending the activity in this way does not seem to call the OnStop(); OnDestroy(); methods.

这可能只是我是愚蠢的,没有看到这样做的一个简单的方法,但有一种方法来访问活动,并呼吁activity.finish();而不必参考一路传递给它呢?

This might just be me being silly and not seeing an easy way of doing this but is there a way to access the Activity and call activity.finish(); without having to pass the reference to it all the way down?

这可能是更小的机器人的问题,更多的是一般的Java问题?对不起,我有点生疏,在两者。也许如果有人能大致解释他们是如何在他们的应用程序处理这样的问题。

This is probably less of an android question and more a general java problem? Sorry I am a little rusty at both. Maybe if someone could explain roughly how they handle an issue like this in their app.

推荐答案

您确实需要直接从您的渲染线程的线程遵守安全规则,而不是调用activity.finish()。处理这种情况最好的办法是发布可运行返回到对UI线程事件队列。而让该Runnable调用activity.finish()。

You do need to obey thread safety rules and not call activity.finish() directly from your render thread. The best way to handle this is to post a runnable back onto the event queue for the UI Thread. And let that Runnable call activity.finish().

您不必往下传递活动,你打算停止活动的区域。下面是我会怎么做。通过活动,你的onCreate实例化的类()。是这样的:

You don't have to pass the activity down to the area where you plan on stopping the activity. Here is what I'd do. Pass the activity to the class you instantiate in onCreate(). Something like:

public void onCreate( ... ) {
   MyRenderer renderer = new MyRenderer( glSurface, this );
}

然后在里面MyRenderer我会做这样的事情:

Then inside MyRenderer I'd do something like:

public void someMethodInRenderer() {
   if( stop ) {
      stop();
   }
}

public void stop() {
   Handler handler = new Handler();
   handler.post( new Runnable() {
      public void run() {
         activity.finish();
      }
   } );
}

注意用回发到UI线程的处理程序。这使得它调用安全activity.finish()。我找不到在文档的任何具体信息,说明它是安全的还是不安全调用完成()从另一个线程,以便为安全起见后回来。

Notice the Handler used to post back to the UI thread. That makes it safe to call activity.finish(). I couldn't find any specific information in the docs stating it's safe or not safe to call finish() from another thread so to be on the safe side post it back.

事情要记住。如果someMethodInRenderer()是在你的程序的肠子深处,那么你不必直接有活动实例访问。你只需要这样的东西,最终调用到活动结束的参考。因此,也许有一个引用您传递到该方法,您可以添加停止系统()的另一部分。因此,停止()和someMethodInRenderer()可能是在同一个班级,或在不同的班级。这是一个选择,你将不得不作出。最终,这是一个结构问题,你必须决定。

Things to keep in mind. If someMethodInRenderer() is deep within the bowels in your program then you don't have to have access directly to the activity instance. You just need a reference so something that eventually calls to the activity to finish. So maybe there is a reference to another part of the system you are passing down to that method where you can add the stop(). So stop() and someMethodInRenderer() could be in the same class, or in different classes. That's a choice you'll have to make. Eventually this is an architecture issue you have to decide.

这篇关于Android的 - 从渲染线程中结束活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆