Anutrix
Anutrix

Reputation: 273

Is it safe to call Screen.hide() after Screen.dispose() in LibGDX?

In libgdx, screen.setScreen() doesn't call dispose automatically right. Inside a overridden setScreen, do I have to call screen.dispose first and then call super.setScreen or call the later first? I know this may seem like duplicate question but still I wanted to know because super.setScreen calls screen.hide. Is calling hide after dispose run-time safe? Is it a bad practice? I am making a 3D game based on this and this example . Here I am extending GameName class by Game and trying to override setScreen so as to call dispose if screen is not null and then call super.setScreen .

Upvotes: 0

Views: 640

Answers (2)

AAryan
AAryan

Reputation: 20140

Two type of disposable assets(SpriteBatch, Texture, Stage, ...), one is shared on different screen and another one is specific for particular screen.

  1. Shared Assets should be disposed from Game's dispose() method.

  2. Screen specific assets should be disposed through dispose() method of Screen but screen's dispose() never called so you need to call it explicitly.

When you change your screen hide() method of Screen called so you should call dispose() method from hide() method.

Upvotes: 0

Tenfour04
Tenfour04

Reputation: 93581

screen.dispose() is never called by LibGDX. You must do it manually yourself before dropping the reference to your screen. If you don't plan to reuse the Screen instance, having screen.hide() call screen.dispose() is the perfect place to do it.

screen.hide() is never called in response to Android events.

I don't recommend overriding game.setScreen() to dispose of screens unless you know for sure you never want to reuse any screen instances. In most simple games, you do want to reuse them rather than waste time unloading and reloading assets repeatedly.

Upvotes: 1

Related Questions