TomGrill / gdx-facebook

libGDX extension providing cross-platform support for Facebook Graph API.
Apache License 2.0
59 stars 21 forks source link

Incompatibility with FragmentActivity #51

Closed Zarandor closed 7 years ago

Zarandor commented 7 years ago

Hi, I'm trying to integrate an ads provider for my android game and it requires that I change my Activity (AndroidApplication) for FragmentActivity and when it comes to test I had a ClassCastException.

Here is the error message : java.lang.ClassCastException: com.mycompany.mygame.AndroidLauncher$GameFragment cannot be cast to com.badlogic.gdx.backends.android.AndroidApplication de.tomgrill.gdxfacebook.android.AndroidFacebookLoader.load(AndroidFacebookLoader.java:30) de.tomgrill.gdxfacebook.core.ReflectionLoader.load(ReflectionLoader.java:53) de.tomgrill.gdxfacebook.core.GDXFacebookSystem.install(GDXFacebookSystem.java:16)

I followed the instruction here to convert to FragmentActivity: https://github.com/libgdx/libgdx/wiki/Starter-classes-%26-configuration#fragment-based-libgdx

When I deactivate the use of gdx-facebook the game runs fine. So how can I make the lib compatible with FragmentActivity ?

And will this issue occur with gdx-twitter too ?

TomGrill commented 7 years ago

I made some changes and pushed to snapshots. Can you verify whether '1.4.1-SNAPSHOT' is working with Fragments?

TomGrill commented 7 years ago

gdx-twitter is not affected since this extension still uses an older loading approach.

Zarandor commented 7 years ago

Ok, so there's no more ClassCastException but the login phase doesn't end. I can see the facebook login dialog, plus the one that confirms that I already granted authorization for my game. But after the closing of that dialog, nothing. onSuccess is not called and none of the other callbacks too. It's due to the use of Fragment because by using AndroidApplication again with the snapshot lib it works.

TomGrill commented 7 years ago

Ok, I will have so setup a Fragment project and test my self. Was hoping the changes were enough.

TomGrill commented 7 years ago

Ok this will fix it for now: Make your GameFragment a feild and overwrite onActivityResult like this:

private GameFragment gameFragment;
...................
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        gameFragment.onActivityResult(requestCode, resultCode, data);
    }

Well I cannot think of a solution to do this step made within the extension, so I added it ti the wiki and release 1.4.1

Zarandor commented 7 years ago

Hi TomGrill,

thank you for your quick return on this issue.

Unfortunately it didn't work for me. I have to focus on an update of my game for now. So I won't integrate that ad provider in it and will have to figure it out by mysfelf after the release of the update.

Can you tell me why for you the solution is in the onActivityResult? If it doesn't work for me, maybe it's a configuration of my activity in the manifest file.

TomGrill commented 7 years ago

It seems that the the Facebook SDK for Android is called from the main Activity from your app. FragmentActivity is only a "child" of the main Activity, thats why you have to manually pass the onActivityResult to the FragmentActivity.

You can have a look at the https://github.com/TomGrill/gdx-facebook-app AndroidLauncher. There is commented section for using FragmentActivity. This works for me.

I did not look further into the details and how this could be avoided.

Tokenyet commented 7 years ago

Thanks! The solution works for me! In my project, I use ViewPager and some fragments, and the logger just gave me the log:

D/gdx-facebook (1.4.1): Could not load existing accessToken.
D/gdx-facebook (1.4.1): Starting GUI sign in.

Finally get the access token until I add the solution that post by TomGrill ! What your AndroidLauncher.java should look like:

public class AndroidLauncher extends FragmentActivity implements  AndroidFragmentApplication.Callbacks {
public static GameFragment libgdxFragment = new GameFragment();
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Fragment codes, see libgdx wiki about Fragment
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        libgdxFragment.onActivityResult(requestCode, resultCode, data);
    }
}

But I'm not sure, if the class/level name field in Facebook api page should change to package.GameFragment or just package.AndroidLauncher.