Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: include FragmentActivity example on Android #47

Open
Adriankhl opened this issue Jun 18, 2021 · 2 comments
Open

Suggestion: include FragmentActivity example on Android #47

Adriankhl opened this issue Jun 18, 2021 · 2 comments

Comments

@Adriankhl
Copy link

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.

@tommyettinger
Copy link
Member

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.

@Adriankhl
Copy link
Author

Adriankhl commented Sep 2, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants