How do I change Activity after logging in with facebook?

Asked

Viewed 87 times

1

After loginfacebook(Fragment) I would like to change from Activity, for example to register, as I do?

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

}

  • I don’t understand very well. You want after logging in via facebook, go to another Activity?

1 answer

0


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

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

To perform the function you need to call your onActivityResult creating a condition for redirecting to Activity you wish. 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 sure 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

It is necessary to obtain the facebook_app_id in which you can generate from the 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"/>

Inclusion of 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!

  • That’s right, but still does not open another Facebook after login

  • @Diegonoceli edits your question with the code you entered there so I can see what you did! Here works normally!

  • Modified, logs in but does not change Fragment

  • @Diegonoceli here worked normal. I’ll do another test and give you a feedback.

  • @Diegonoceli did several tests and worked beauty! I put the full code for you to do a test there. Hugs. Good Luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.