简体   繁体   English

我何时以及为何应该装载和处置资产?

[英]When and why should I load and dispose assets?

I load my assets in create and dispose them pause . 我将资产加载到createpause它们。

But when a game is closed, the process may or may not terminate. 但是,当游戏关闭时,该过程可能会终止,也可能不会终止。 ie, it could be just the activity that got stopped and not the process. 即,可能只是停止的活动,而不是过程。 In this case, when the game is quickly reopened, create will not be called. 在这种情况下,当快速重新打开游戏时,将不会调用create Only resume will be called. resume将被调用。 Then, shouldn't I be reloading my assets in resume also? 然后,我是否也应该在resume重新加载我的资产?

The initialization using life cycle methods of an activity in Android goes like this: 使用Android中活动的生命周期方法进行的初始化如下:

  • onCreate() -> onDestroy() onCreate()-> onDestroy()
  • onStart() -> onStop() onStart()-> onStop()
  • onResume() -> onPause() onResume()-> onPause()

Meaning that, for example, what I init in onCreate, I should get rid off in onDestroy. 这意味着,例如,我在onCreate中初始化的内容应该在onDestroy中摆脱。 And same for all three couple of methods. 这三种方法都相同。

In your case, I would load the assets in onStart and dispose them in onStop. 在您的情况下,我将资产加载到onStart并将其处置在onStop中。

Also, please note that onDestroy method may not be called. 另外,请注意,可能不会调用onDestroy方法。

not if I understand your question, if you ask why? 不,如果我理解您的问题,请问为什么? should the dispose-law, it is common that android is responsible for releasing the resources, lidGDX. 根据处理法,通常由android负责释放资源lidGDX。

in some cases it creates objects out of reach of the garbage collector, of the JVM, these objects are created from native side of the application and GC does not have the ability to remove them, so you have to call the objects that have dispose not all objects have the need to use the dipose, here you can see a list: 在某些情况下,它创建的对象超出了垃圾回收器,JVM的范围,这些对象是从应用程序的本机端创建的,GC无法删除它们,因此您必须调用未处理的对象所有对象都需要使用dipose,在这里您可以看到一个列表:

http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/Disposable.html

It may also be part of this answer helps you: 它也可能是此答案的一部分,可以帮助您:

Game crash if interrupted while Splash Screen is on - LIBGDX 如果在“启动画面”打开时中断游戏崩溃-LIBGDX

I apologize for my English, and if this question does not help you tell me and delete 我为我的英语致歉,如果这个问题不能帮助您告诉我,请删除

Dispose of your assets in dispose() or when you are done with them and need to free up memory (such as switching to a new stage that uses different assets). dispose()或完成处理后需要释放资产(例如,切换到使用其他资产的新阶段dispose()时,请dispose()资产。

The game's dispose method is called in the Activity's onPause() method, but only if the Activity is being finished, so you are safe if you only dispose in dispose() . 游戏的dispose方法是在Activity的onPause()方法中调用的,但是仅当Activity完成时才调用,因此,如果仅在dispose()处置,则是安全的。 If the application process remains open, dispose() has been called, so you're good. 如果应用程序进程保持打开状态,则已调用dispose() ,所以您很好。 And at this point, if your game reopens, create() will be called again so you don't need to worry about reloading in resume() . 至此,如果您的游戏重新打开,将再次调用create() ,因此您无需担心在resume()重新加载。

If for some reason Android force-quits your game, then all the memory is cleared up anyway and you have no need to worry about leaks. 如果由于某种原因Android强制退出了您的游戏,那么无论如何,所有内存都将被清除,您无需担心泄漏。

And the reason why you must dispose in dispose() is for cases where the user backs out of your game Activity but the Application process is not shut down. 而必须在dispose()中进行处置的原因是,当用户退出游戏活动,但应用程序进程未关闭时。 If the user launches the game Activity again, all the non-disposed assets from the previous instance are still held in memory and have now leaked. 如果用户再次启动游戏活动,则来自先前实例的所有未处置资产仍保留在内存中,并且现在已泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM