Call Activity after Facebook login

Asked

Viewed 789 times

4

I’m new in the world Android and I’m in need of some help.
I can’t make the call to a new Activity after the facebook login.

The login is running correctly and when it is successfully performed, it returns the same Activity with the button to Logout. I tried to make the call by Intent, but it didn’t work. Could you help me, please.

I’m using the facebook sdk 4.0

Below is the code of main Ragment:

public class MainFragment extends Fragment {

    private CallbackManager callbackManager;
    private TextView textView;
    private AccessTokenTracker accessTokenTracker;
    private ProfileTracker profileTracker;

    private FacebookCallback<LoginResult> callback = new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();
            Profile profile = Profile.getCurrentProfile();
            displayMessage(profile);


        }


        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException e) {

        }
    };

    public MainFragment() {

    }


    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getActivity().getApplicationContext());

        callbackManager = CallbackManager.Factory.create();

        accessTokenTracker= new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldToken, AccessToken newToken) {

            }
        };

        profileTracker = new ProfileTracker() {
            @Override
            protected void onCurrentProfileChanged(Profile oldProfile, Profile newProfile) {
                displayMessage(newProfile);
            }
        };

        accessTokenTracker.startTracking();
        profileTracker.startTracking();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        LoginButton loginButton = (LoginButton) view.findViewById(R.id.login_button);
        textView = (TextView) view.findViewById(R.id.textView);

        loginButton.setReadPermissions("user_friends");
        loginButton.setFragment(this);
        loginButton.registerCallback(callbackManager, callback);

    }

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

        Intent intent = new Intent();


    }

    private void displayMessage(Profile profile){
        if(profile != null){
            textView.setText(profile.getName());
        }
    }

    @Override
    public void onStop() {
        super.onStop();
        accessTokenTracker.stopTracking();
        profileTracker.stopTracking();
    }

    @Override
    public void onResume() {
        super.onResume();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);
    }
}

2 answers

1

To do the redirect, you can check if the user is logged and whether the token this same user has already expired. In this case, it would look like this below. See:

public void onSuccess(LoginResult loginResult) {
    AccessToken accessToken = loginResult.getAccessToken();
    Profile profile = Profile.getCurrentProfile();
    displayMessage(profile);

    boolean isLoggedIn = accessToken != null && !accessToken.isExpired();

    if(isLoggedIn){
      // aqui dentro você deve chamar sua activity.
    }   
}

-1

Would that be?

   @Override
    public void onSuccess(LoginResult loginResult) {
        AccessToken accessToken = loginResult.getAccessToken();
        Profile profile = Profile.getCurrentProfile();
        displayMessage(profile);
        startActivity(getContext(), NovaActivity.class);
    }
  • Hi, Androiderson, I’ve tried it this way and I get an error: Error:(40, 13) error: method startActivity in class Fragment cannot be Applied to Given types; required: Intent found: Context,Class<Stationfragment> Reason: actual and formal argument lists differ in length

  • Are you sure that your NovaActivity.class is a Activityand not a Fragment?

  • Now I’ve arranged for the main Activity, but when it returns it gets the logout button on the screen... the code looks like this: public void onSuccess(Loginresult loginResult) { Accesstoken accessToken = loginResult.getAccessToken(); Profile profile profile = Profile.getCurrentProfile(); displayMessage(profile); Intent Intent = new Intent(getcontext(), Stationactivity.class); startActivity(Intent);

  • and when starting the app again it drops to the logout screen and not to the main Activity

  • Every time the user lands on the login screen, and the user is already logged in, you have to redirect to the correct screen. This is not automatic, you have to implement.

  • In this case I should use the Lifecycle from facebook sdk itself?

  • 1

    No, facebook only tells you whether or not the user is logged in, the rest is up to you.

Show 2 more comments

Browser other questions tagged

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