LoginButton with native Fragment

只谈情不闲聊 提交于 2019-12-01 14:43:53

问题


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.


回答1:


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);
    }
}



回答2:


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.




回答3:


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)




回答4:


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




回答5:


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.



来源:https://stackoverflow.com/questions/24644599/loginbutton-with-native-fragment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!