Twitter Login from fabric doesn't work in frag

2019-09-11 09:17发布

Authorization completed with an error

10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: com.twitter.sdk.android.core.TwitterAuthException: Authorize failed. 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at com.twitter.sdk.android.core.identity.TwitterAuthClient.handleAuthorize(TwitterAuthClient.java:110) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at com.twitter.sdk.android.core.identity.TwitterAuthClient.authorize(TwitterAuthClient.java:101) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at com.twitter.sdk.android.core.identity.TwitterLoginButton$LoginClickListener.onClick(TwitterLoginButton.java:161) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.view.View.performClick(View.java:5198) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.view.View$PerformClick.run(View.java:21147) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.os.Handler.handleCallback(Handler.java:739) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.os.Handler.dispatchMessage(Handler.java:95) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.os.Looper.loop(Looper.java:148) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at android.app.ActivityThread.main(ActivityThread.java:5417) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at java.lang.reflect.Method.invoke(Native Method) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 10-26 11:58:33.404 7456-7456/com.idfcwalletapp.idfc_wallet E/Twitter: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

and my code for fragment is:

public class FragmentSample extends Fragment {
    private TwitterLoginButton loginButton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       View rootView=inflater.inflate(R.layout.fragment_sample,container,false);
        loginButton = (TwitterLoginButton)rootView. findViewById(R.id.twitter_login_button);

        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
        Fabric.with(getActivity(), new Twitter(authConfig));

        loginButton.setCallback(new Callback<TwitterSession>() {
            @Override
            public void success(Result<TwitterSession> result) {
                // The TwitterSession is also available through:
                // Twitter.getInstance().core.getSessionManager().getActiveSession()
                TwitterSession session = result.data;
                // TODO: Remove toast and use the TwitterSession's userID
                // with your app's user model
                String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")";
                Toast.makeText(getActivity(), msg, Toast.LENGTH_LONG).show();
            }

            @Override
            public void failure(TwitterException exception) {
                Log.d("TwitterKit", "Login with Twitter failure", exception);
            }
        });
    }

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

    }
}

1条回答
太酷不给撩
2楼-- · 2019-09-11 09:48

Though late here is the solution.

onActivityResult of the fragment doesnt gets called directly. It invokes the onActivityResult of your activity which calls your fragment. So override onActivityResult on your activity first as below,

fragment = getSupportFragmentManager().findFragmentByTag(title);
if (fragment != null) {
fragment.onActivityResult(requestCode, resultCode, data);
}

Hope it works :)

查看更多
登录 后发表回答