如何将 Twitter Fabric Session 从 Activity 发送到另一个 Activity?
How to send Twitter Fabric Session from Activity to Another Activity?
我想将 Twitter 会话从 activity 发送到另一个 activity.I 已搜索(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");
}
你不能这样通过。 TwitterSession
不可序列化,因此您不能使其成为可序列化 class.
的成员
由于 TwitterSession 是单例的,您可以简单地从 MainActivity 中调用以下方法
TwitterSession session = Twitter.getSessionManager().getActiveSession();
所以你的 activity 代码看起来像
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TwitterSession session = Twitter.getSessionManager().getActiveSession();
}
在您的 MainActivity 中:
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);
}
我想将 Twitter 会话从 activity 发送到另一个 activity.I 已搜索(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");
}
你不能这样通过。 TwitterSession
不可序列化,因此您不能使其成为可序列化 class.
由于 TwitterSession 是单例的,您可以简单地从 MainActivity 中调用以下方法
TwitterSession session = Twitter.getSessionManager().getActiveSession();
所以你的 activity 代码看起来像
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TwitterSession session = Twitter.getSessionManager().getActiveSession();
}
在您的 MainActivity 中:
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);
}