Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LoginButton with native Fragment

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.

like image 348
miguel Avatar asked Jul 09 '14 02:07

miguel


People also ask

How to add a login feature to a React Native application?

In android/app/build.gradle, under android -> defaultConfig, add: Finally, start a virtual device (or plug in your phone), and run react-native run-android. This tutorial showed you how to add a login feature to a React Native application. You learned that OAuth 2.0 is an authorization protocol, and OIDC is an authentication layer on top of it.

Is Register screen a fragment or an activity?

Well for starters your register screen is an Activity and not a Fragment.. And where do you want to start it from ? I dont see Login or Register any where in your MainActivity.

What is the LoginButton in Microsoft identity platform?

The LoginButton is both a button and flyout control to facilitate Microsoft identity platform authentication. It provides two states: When the user is not signed in, the control is a simple button to initiate the sign in process. When the user is signed in, the control displays the current signed in user name, profile image, and email.

How does A NativeScript login form work?

Let’s start diving into how this works by looking at the user interface components that make up this page. The main part of the login form uses the NativeScript core theme’s class names to render the page’s inputs and buttons.


2 Answers

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);
    }
}
like image 158
Jonas Lüthke Avatar answered Oct 19 '22 06:10

Jonas Lüthke


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.

like image 20
funcoder Avatar answered Oct 19 '22 04:10

funcoder