gdx-liftoff
gdx-liftoff copied to clipboard
Suggestion: include FragmentActivity example on Android
In my kotlin project, libgdx is used for client graphical interface, where the main game logic is launched and ran by kotlin coroutine. This is pretty straightforward for desktop, but not so much for Android. I spent some time to realize that I can use lifecycleScope for Android (https://developer.android.com/topic/libraries/architecture/coroutines#lifecyclescope).
However, the android template generated by this wizard and some other open source projects that I referred to rely on the default AndroidApplication
class, and I spend a pretty long time to realize that this doesn't work nicely with lifecycleScope. Where a simple AndroidFragmentApplication
with FragmentActivity
(https://github.com/libgdx/libgdx/wiki/Starter-classes-and-configuration#game-fragment) works flawlessly in my case.
Therefore, I suggest that including a template of FragmentActivity
may help other people to set things up quicker.
Sorry for my late reply... I don't do much Android development, and haven't done anything with fragments. A template might be good, but I'm not sure I should be the one to write it, since I am not at all confident with some of the Android-specific code. If you have or can extract a skeleton project from your code, and can post it here, I could probably get that into a template. Or you could submit a PR, either way would be appreciated. I'm kind of lost with the FragmentActivity code in the wiki; I really haven't done much Android dev at all.
Hi, so basically in AndroidLauncher.kt
, instead of AndroidLauncher
extending AndroidApplication
there should be a AndroidFragmentApplication
(from libgdx) and a FragmentActivity
(from Android sdk):
class YourGameFragment() : AndroidFragmentApplication() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return initializeForView(YourGame())
}
}
where YourGame
is a libgdx Game()
(I believe ApplicationAdapter
also works).
class AndroidLauncher : FragmentActivity(), AndroidFragmentApplication.Callbacks {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val yourGameFragment = YourGameFragment()
val trans: FragmentTransaction = supportFragmentManager.beginTransaction()
trans.replace(android.R.id.content, yourGameFragment)
trans.commit()
}
override fun exit() {
}
}
the class name in AndroidManifest.xml
should point to this. I don't know if the AndroidFragmentApplication.Callbacks
is necessary or not but this is suggested by the libgdx wiki.
I think there should also be an additional dependency in the build.gradle
: androidx.fragment:fragment-ktx:${Versions.androidFragmentKtxVersion}
I hope I have provided sufficient information, it is a bit difficult for me to submit a PR since I don't know how the template works here.