How do I change the activity after logging in with facebook?

1

After the loginfacebook (fragment) I would like to change the activity, for example to register, how do I?

public class LoginFacebook extends Fragment {

private LoginButton loginButton;
private CallbackManager callbackManager;
private AccessTokenTracker accessTokenTracker;
private AccessToken accessToken;
private Fragment1 fragment1;

@Override
public View onCreateView(
        final LayoutInflater inflater,
        final ViewGroup container,
        Bundle savedInstanceState) {
    FacebookSdk.sdkInitialize(getContext());

    callbackManager = CallbackManager.Factory.create();
    final View view = inflater.inflate(R.layout.loginfacebook, container, false);

//

   loginButton = (LoginButton)view.findViewById(R.id.loginfacebook);
    loginButton.setFragment(this);
    loginButton.setReadPermissions(Arrays.asList("email"));
    loginButton.setReadPermissions(Arrays.asList("public_profile"));



    // Callback registration
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
            dlg.setMessage("deu"+loginResult.getAccessToken().getUserId());
            dlg.setNeutralButton("ok",null);
            dlg.show();


        }


        @Override
        public void onCancel() {
            AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
            dlg.setMessage("cancelou");
            dlg.setNeutralButton("ok",null);
            dlg.show();

        }

        @Override
        public void onError(FacebookException exception) {
            AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
            dlg.setMessage("deu erro");
            dlg.setNeutralButton("ok",null);
            dlg.show();

        }
    });


    accessTokenTracker = new AccessTokenTracker() {
        @Override
        protected void onCurrentAccessTokenChanged(
                AccessToken oldAccessToken,
                AccessToken currentAccessToken) {
            // Set the access token using
            // currentAccessToken when it's loaded or set.
        }
    };






    return view;

}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
    if(isLoggedIn()){
        Intent intent = new Intent(getContext(), Cadastro.class);
        startActivity(intent);

}

public boolean isLoggedIn() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null;
}

}

    
asked by anonymous 29.08.2016 / 20:59

1 answer

0

As I understand your question, you can create a function to check if it is logged by checking the token generated with getCurrentAccessToken() through the class AccessToken . If there is token registered, it will return true, otherwise it will return false. Result:

public boolean isLoggedIn() {
    AccessToken accessToken = AccessToken.getCurrentAccessToken();
    return accessToken != null;
}

To execute the function you need to call your onActivityResult creating a condition for redirection to Activity you want. Example:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(isLoggedIn()){
        Intent intent = new Intent(getContext(), OutraActivity.class);
        startActivity(intent);
    }
}

Details

Complete Code

Although it has all the explanations in the documentation, I made a point to publish here an example of an application.

Activity

public class MainReadForm extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new LoginFragment())
                .commit();

    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:background="@android:color/white"
        android:layout_height="match_parent"
        android:orientation="vertical">
    </LinearLayout>

</LinearLayout>

Fragment

public class LoginFragment extends Fragment {

        private LoginButton loginButton;


   private CallbackManager callbackManager;

    @Override
    public View onCreateView(
            final LayoutInflater inflater,
            final ViewGroup container,
            Bundle savedInstanceState) {
        FacebookSdk.sdkInitialize(getContext());

        callbackManager = CallbackManager.Factory.create();
        final View view = inflater.inflate(R.layout.login_activity, container, false);

        loginButton = (LoginButton) view.findViewById(R.id.login_button);
        loginButton.setFragment(this);
        loginButton.setReadPermissions(Arrays.asList("email"));
        loginButton.setReadPermissions(Arrays.asList("public_profile"));


        // Callback registration
        loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
                dlg.setMessage("deu" + loginResult.getAccessToken().getUserId());
                dlg.setNeutralButton("ok", null);
                dlg.show();
            }


            @Override
            public void onCancel() {
                AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
                dlg.setMessage("cancelou");
                dlg.setNeutralButton("ok", null);
                dlg.show();

            }

            @Override
            public void onError(FacebookException exception) {
                AlertDialog.Builder dlg = new AlertDialog.Builder(getContext());
                dlg.setMessage("deu erro");
                dlg.setNeutralButton("ok", null);
                dlg.show();

            }
        });
        return view;

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
        if (isLoggedIn()) {
            Intent intent = new Intent(getContext(), Search.class);
            startActivity(intent);
        }

    }
    public boolean isLoggedIn() {
        AccessToken accessToken = AccessToken.getCurrentAccessToken();
        return accessToken != null;
    }
}

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal"
    >
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="5dip"
        android:textSize="14pt"
        android:text="hello world"
        />

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="30dp"
        android:layout_marginBottom="30dp" />

</LinearLayout>

Strings

You need to get the facebook_app_id you can generate from Quick Start for Android .

<string name="facebook_app_id">seu app id aqui</string>

Manifest

Adding permission for internet access

<uses-permission android:name="android.permission.INTERNET"/>

Including the meta-data element

<application android:label="@string/app_name" ...>
    ...
    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    ...
</application>

Good Luck!

    
29.08.2016 / 21:39