在使用TwitterCore实例化Twitter登录的Twitter文档中,有两个可行的位置可以创建成功的活动:
*直接在回调中(需要私有实例变量mContext作为上下文) *在onActivityResult中(在登录活动中,并且需要将其作为上下文调用)
有首选方法吗?任何一方的利弊是什么?
Android Callback v1(来自loginButton.callback声明):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_twitter_session);
loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
loginButton.setCallback(new Callback<com.twitter.sdk.android.core.TwitterSession>() {
@Override
public void success(Result<com.twitter.sdk.android.core.TwitterSession> twitterSessionResult) {
Log.i("SUCCESS", twitterSessionResult.data.toString());
Intent intent = new Intent(mContext, MainActivity.class);
startActivity(intent);
}
@Override
public void failure(TwitterException e) {
Log.e("ERROR", e.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
}
Android Callback v2(来自onActivityResult):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET);
Fabric.with(this, new Twitter(authConfig));
setContentView(R.layout.activity_twitter_session);
loginButton = (TwitterLoginButton) findViewById(R.id.login_button);
loginButton.setCallback(new Callback<com.twitter.sdk.android.core.TwitterSession>() {
@Override
public void success(Result<com.twitter.sdk.android.core.TwitterSession> twitterSessionResult) {
Log.i("SUCCESS", twitterSessionResult.data.toString());
}
@Override
public void failure(TwitterException e) {
Log.e("ERROR", e.toString());
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
if (resultCode == -1){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}else{
Log.i("On activity result resultCode", String.valueOf(resultCode));
Log.i("On activity result requestCode", String.valueOf(requestCode));
Log.i("On activity result data", data.toString());
}
}