2
\$\begingroup\$

Game works fine on the initial launch on Android, but after that restarting game does not render the graphics on the screen properly.

It is a copy of the question Sprites not being rendered on screen change - Android Libgdx

but there is no solution yet for the question

\$\endgroup\$
2
\$\begingroup\$

Most probably that's because of the loss of OpenGL context and the way Android handles Activity finish (process/task of the app may not be destroyed when main activity is closed).

Check out this Stackoverflow answer for more details:

https://stackoverflow.com/questions/12508769/libgdx-app-exit-on-android-not-closing-application

Quote:

You have rediscovered the mismatch between the lifetime of Java objects (tied to the life of the application process) and the lifetime of texture objects (tied to the life of the OpenGL context which is tied to the visibility of the Activity).

On app "exit", just the Activity is exited, and Android is caching the process in the background. When you "restart" the app Android just starts a new Activity in the same process. In this case the Activity is finding a valid Java Texture object, but the underlying bytes it "points to" in the OpenGL context are gone (since the OpenGL context is invalidated when the Activity is no longer visible).

The fix is to re-load textures on activity creation. You must make sure all your objects that contain textures (and objects that contain objects that contain textures, etc) are tied to the Activity lifecycle. Generally this means avoiding static variables (which are part of the application lifecycle), but you can jump through hoops to invalidate and re-initialize globals if you want.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ ^ Thank you . I will check it the way you mentioned and update it soon. \$\endgroup\$ – Yogesh Sharma Jun 9 '15 at 13:55
  • 1
    \$\begingroup\$ Yes, it worked.. :D \$\endgroup\$ – Yogesh Sharma Jun 9 '15 at 15:05
  • \$\begingroup\$ @YogeshSharma If this solved your question, you might want to mark this answer as the accepted one. \$\endgroup\$ – Vaillancourt Jan 15 '18 at 15:28
0
\$\begingroup\$

You can make a very simple fix for this problem. If the application process after its closure is unnecessary, you can simply kill it. In this case, when you restart the application, it will have to re-initialize the static variables.

public class AndroidLauncher extends AndroidApplication {

    //... some code ...

    @Override
    protected void onDestroy() {
        android.os.Process.killProcess(android.os.Process.myPid()); // this line will kill process
        super.onDestroy();
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.