Login button: Not passing to success after onActivityResult() in fragment

android
login

#1

Hello everyone!
I’ve walked throughout all steps described here https://dev.twitter.com/twitter-kit/android/twitter-login, but it still not returning me to success in callback after fragment.onActivityResult().
Please help me.

Activity code:

public class LoginActivity extends ActionBarActivity {

public static String TAG = "LoginActivity";

@Override
protected void onCreate(Bundle savedInstanceState) {

        setTheme(R.style.MainActivityTheme);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        getSupportFragmentManager().beginTransaction().replace(R.id.container, new LoginFragment()).commit();
}

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

    Fragment f = getSupportFragmentManager().findFragmentById(R.id.container);

    if(f != null) { 
      
      f.onActivityResult(requestCode, resultCode, data); 
    }

Fragment code:

public class LoginFragment extends Fragment implements LoginView.LoginViewListener {
  

public static final String TAG = "LoginFragment";

public TwitterLoginButton mTwitterLogin;

private LoginView mView;

@Override
public View onCreateView(final LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

mView = (LoginView) inflater.inflate(R.layout.fragment_login, null); 

mTwitterLogin = (TwitterLoginButton)mView.findViewById(R.id.loginTwitter);
mTwitterLogin.setCallback(new Callback<TwitterSession>() {

        @Override
        public void success(Result<TwitterSession> result) {
            // Do something with result, which provides a
            // TwitterSession for making API calls
            TwitterSession data = result.data;
            data.getUserName();
        }

        @Override
        public void failure(TwitterException exception) {
            // Do something on failure
            }
        }
    });
    return mView;
}

#2

Hi there!

I’ve already solved my problem! I forgot to write this code in my Fragment.

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

#3

Fantastic, thanks for sharing the solution too :white_check_mark:


#4

#5