I\'m trying to implement the Facebook LoginButton using the tutorial here https://developers.facebook.com/docs/android/login-with-facebook/v2.0#step2
The problem is
The reason is obvious. Facebook SDK is using the Fragment class from the support library for making it backward compatible and work with older versions of android (API Level < 11). And you must use the same class in your application as well.
Include the support library android-support-v4.jar in to the libs folder and use the Fragment class from it (for Ant based projects). If you are using Gradle build system, Follow the instruction given in Lei's answer above.
Update: If your application have high dependency with native library, then you are left with a single option. The Facebook SDK code is available here. Fork it and change the SDK to use native library (Remove the support library from the SDK itself). But keep in mind that, your application will be limited to run on API level greater than 10 (minSdkVersion should be 11).
I think the solution you are looking for is the wrapper class below. Using this you can just call
authButton.setFragment(new NativeFragmentWrapper(this));
The wrapper is a support fragment and just passes the method calls from the facebook LoginButton to the native fragment. I'm using this and it works fine.
public class NativeFragmentWrapper extends android.support.v4.app.Fragment {
private final Fragment nativeFragment;
public NativeFragmentWrapper(Fragment nativeFragment) {
this.nativeFragment = nativeFragment;
}
@Override
public void startActivityForResult(Intent intent, int requestCode) {
nativeFragment.startActivityForResult(intent, requestCode);
}
@Override
public void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
nativeFragment.onActivityResult(requestCode, resultCode, data);
}
}
I solved this issue by using the activity context instead of the fragment context and passing the "onActivityResult" from the Activity, to the fragment to the callbackManager.
Just follow the following instructions:
Add the following method inside the activity of your fragment, to catch and pass any result to the fragment:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
final MyFragment fragment = (MyFragment) getFragmentManager().findFragmentById(R.id.container);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}
}
Override onActivityResult also inside your fragment which contains the LoginButton and pass the result to the callbackManager.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
I hope it works.
Follow these steps:
Remove import android.app.Fragment;
Add import android.support.v4.app.Fragment;
Right click on your Project and Select properties.
Build Path > Library > Add External Library > Locate your android-support-v4.jar file(sdk > extras > android > supports > v4)
Add this below in you build.gradle file
dependencies {
compile 'com.android.support:support-v4:+'
}
Then import android.support.v4.app.Fragment instead of the native fragment.