Redirect to an new Activity after login in to Facebook (Facebook SDK 3.5.2)

+2 votes
asked Nov 19, 2013 by dmt82

I have two classes that is for Facebook login, and it works. I can press the Loginbutton, write in my username/password and it will log me in, BUT the thing is that I will come back to the Main-class (startsite) with my logo picture and so on.

What I want to do is, when you pressed the loginbutton and logged in, you will be redirected to a new Activity

Here is my 2 Classes you can see, but where do I do the redirect? Read on StackOverflow about the "onComplete" but I cant make it, think its because I use SDK 3.5.2 and not 2.0. So How can i make a redirect to a new Activity after success login.

My MainActivity:

public class SocialLoginActivity extends FragmentActivity {

private SocialLoginFragment socialLoginFragment;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        socialLoginFragment = new SocialLoginFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, socialLoginFragment).commit();
    } else {
        // Or set the fragment from restored state info
        socialLoginFragment = (SocialLoginFragment) getSupportFragmentManager()
                .findFragmentById(android.R.id.content);
    }
}

}

And here is the FragmentClass:

public class SocialLoginFragment extends Fragment {

private static final String TAG = "SocialLoginFragment";
private UiLifecycleHelper uiHelper;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_social_login, container,
            false);
    LoginButton authButton = (LoginButton) view
            .findViewById(R.id.authButton);
    authButton.setFragment(this);
    // authButton.setReadPermissions(Arrays.asList("name", "user_birthday",
    // "friends_birthday"));

    return view;
}

private void onSessionStateChange(Session session, SessionState state,
        Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    uiHelper = new UiLifecycleHelper(getActivity(), callback);
    uiHelper.onCreate(savedInstanceState);
}

private Session.StatusCallback callback = new Session.StatusCallback() {
    @Override
    public void call(Session session, SessionState state,
            Exception exception) {
        onSessionStateChange(session, state, exception);
    }
};

@Override
public void onResume() {
    super.onResume();

    Session session = Session.getActiveSession();
    if (session != null && (session.isOpened() || session.isClosed())) {
        onSessionStateChange(session, session.getState(), null);
    }
    uiHelper.onResume();
}

@Override
public void onPause() {
    super.onPause();
    uiHelper.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    uiHelper.onSaveInstanceState(outState);
}

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

}

EDIT

enter image description here

1 Answer

+5 votes
answered Nov 19, 2013 by ming-li

The error you see is because a Fragment is not a Context object. You need to get the parent activity, something like:

Intent intent = new Intent(getActivity(), MainActivity.class);
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...