LoginButton with native Fragment

后端 未结 5 1523
太阳男子
太阳男子 2021-01-17 20:10

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

相关标签:
5条回答
  • 2021-01-17 20:12

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

    0 讨论(0)
  • 2021-01-17 20:13

    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);
        }
    }
    
    0 讨论(0)
  • 2021-01-17 20:15

    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.

    0 讨论(0)
  • 2021-01-17 20:27

    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)

    0 讨论(0)
  • 2021-01-17 20:33

    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.

    0 讨论(0)
提交回复
热议问题