libgdx/libgdx

AndroidFragmentApplication dies onPause

rdeo-aziwell opened this issue · 1 comments

Issue details

Using an AndroidFragmentApplication causes the app to kill itself when onPause is called.
Error log: waiting for pause synchronization took too long; assuming deadlock and killing

Reproduction steps/code

class GameFragment: AndroidFragmentApplication() {
    private val game = Game()

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        return initializeForView(game)
    }

    override fun onPause() {
        // Dies after onPause
        super.onPause()
    }
}

Fragment is used in Compose app like this:

AndroidViewBinding(GameFragmentBinding::inflate)

Workaround

Crash can be avoided by manually calling onDrawFrame

override fun onPause() {
   val job = scope.launch {
      while(coroutineContext.isActive) {
         (Gdx.graphics as AndroidGraphics).onDrawFrame(null)
         yield()
      }
   }
   super.onPause()
   job.cancel()
}

Version of libGDX and/or relevant dependencies

1.12.1

Stacktrace

AndroidGraphics.java(414): Gdx.app.error(LOG_TAG, "waiting for pause synchronization took too long; assuming deadlock and killing");

Please select the affected platforms

  • Android
  • iOS
  • HTML/GWT
  • Windows
  • Linux
  • macOS

Thank you! This answer solved my problem.