LoginButton with native Fragment

2019-02-21 15:27发布

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 on the line authButton.setFragment(this);.

I'm using native fragments (android.app.Fragment) but setFragment expects a support Fragment (android.support.v4.app.Fragment).

EDIT: I cannot switch to support Fragments, I have a big app that uses native Fragments.

5条回答
我只想做你的唯一
2楼-- · 2019-02-21 15:47

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);
    }
}
查看更多
男人必须洒脱
3楼-- · 2019-02-21 16:03

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).

查看更多
等我变得足够好
4楼-- · 2019-02-21 16:03

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.

查看更多
地球回转人心会变
5楼-- · 2019-02-21 16:04

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:

  1. Don't call loginButton.setFragment(fragment). Per default, the Activity context of the LoginButton is used.
  2. 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);
        }
    }
    
  3. 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.

查看更多
该账号已被封号
6楼-- · 2019-02-21 16:04

Follow these steps:

  1. Remove import android.app.Fragment;

  2. Add import android.support.v4.app.Fragment;

  3. Right click on your Project and Select properties.

  4. Build Path > Library > Add External Library > Locate your android-support-v4.jar file(sdk > extras > android > supports > v4)

查看更多
登录 后发表回答