我想将twitter会话从活动发送到另一个活动。我搜索过(How to pass an object from one activity to another on Android)但是序列化的TwitterSession有些错误。该方案是用户成功登录此会话信息到MainActivity。这是我的代码。
我错了什么?
TwitterSessionObject.java
import com.twitter.sdk.android.core.TwitterSession;
import java.io.Serializable;
public class TwitterSessionObject implements Serializable {
private TwitterSession session;
public TwitterSessionObject(TwitterSession twitterSession){
this.session = twitterSession;
}
public TwitterSession getTwitterSession() {
return session;
}
public void setSession(TwitterSession session) {
this.session = session;
}
}
Login.java
@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_login);
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
// Do something with result, which provides a TwitterSession for making API calls
TwitterSession session = Twitter.getSessionManager().getActiveSession();
TwitterAuthToken authToken = session.getAuthToken();
String token = authToken.token;
String secret = authToken.secret;
TwitterSessionObject sessionObject = new TwitterSessionObject(session);
Intent intent = new Intent(Login.this, MainActivity.class);
intent.putExtra("Session", sessionObject);
startActivity(intent);
}
@Override
public void failure(TwitterException exception) {
// Do something on failure
}
});
}
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
TwitterSessionObject session = (TwitterSessionObject)intent.getSerializableExtra("Session");
}
2 个答案:
答案 0 :(得分:3)
你不能那样传递它。 TwitterSession
不可序列化,因此您无法使其成为可序列化类的成员。
由于TwitterSession是单身,因此您只需从MainActivity中调用以下方法
即可
TwitterSession session = Twitter.getSessionManager().getActiveSession();
所以你的活动代码看起来像是
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TwitterSession session = Twitter.getSessionManager().getActiveSession();
}
答案 1 :(得分:0)
中的:
public class MainActivity extends Activity {
private TwitterLoginButton loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
Boolean restoredText = prefs.getBoolean("loginStatus", false);
if (restoredText == true) {
startActivity(new Intent(this, YourTwitterClass.class));
finish(); // destroy login so user can't come back with back button
}
loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button);
loginButton.setCallback(new Callback<TwitterSession>() {
@Override
public void success(Result<TwitterSession> result) {
startActivity(new Intent(this, YourTwitterClass.class));
}
@Override
public void failure(TwitterException exception) {
//do something
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
loginButton.onActivityResult(requestCode, resultCode, data);
}