所以我有facebook登录片段设置,但因为我正在使用SectionsPagerAdapter构建mutli-tab + swipe应用程序,所以我将登录(启动画面)构建到一个新活动中。如何在活动中管理会话?
修改
我要做的是将会话项放在SessionActivity中,并通过使用它的所有活动进行扩展。然后我设置了登录活动并将其作为我的主要入口点。
public class SessionActivity extends FragmentActivity {
private static final String TAG = "SessionActivity";
protected UiLifecycleHelper uiHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
}
public Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Toast.makeText(this, "Logged in...", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, MainActivity.class);
//startActivity(intent);
//finish();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
Toast.makeText(this, "Logged out...", Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
我的登录作为应用程序的主要入口点。这样,一旦用户登录,会话就会启动。
public class FacebookActivity extends SessionActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
}
此时onSessionStateChange应该处理显示登录屏幕或工作活动,但是我在这个例子中陷入无限循环打开和关闭登录。
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Toast.makeText(this, "Logged in...", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(this, MainActivity.class);
//startActivity(intent);
//finish();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
Toast.makeText(this, "Logged out...", Toast.LENGTH_LONG).show();
}
}
答案 0 :(得分:0)
最后,我使用控制SessionActivity作为基础设置我的fb会话,并使用我正在使用的任何活动扩展它。然后我为登录过程设置了FacebookActivity。当扩展了SessionActivity的活动发现它没有fb会话时,它将打开FacebookActivity。如果他们退出FacebookActivity,它将再次打开自己,这使得一个漂亮的闪屏四周。
public class SessionActivity extends FragmentActivity {
protected GraphUser userDetails;
private static final String TAG = "SessionActivity";
protected UiLifecycleHelper uiHelper;
public Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
getFbUserDetails(session);
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
Intent intent = new Intent(this, FacebookActivity.class);
startActivity(intent);
}
}
private void getFbUserDetails(final Session session) {
Log.i(TAG, "Making fb API call...");
// Make an API call to get user data and define a
// new callback to handle the response.
Request request = Request.newMeRequest(session,
new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// If the response is successful
Log.i(TAG, "Processing fb response...");
if (session == Session.getActiveSession()) {
if (user !=null) {
Log.i(TAG, "Setting userDetails...");
userDetails = user;
}
}
if (response.getError() != null) {
// Handle errors, will do so later.
Log.i(TAG, "Error while processing fb response...");
}
}
});
request.executeAsync();
}
@Override
public void onStart() {
super.onStart();
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(this);
}
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if(session==null){
// try to restore from cache
session = Session.openActiveSessionFromCache(this);
}
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
}
}
public class FacebookActivity extends FragmentActivity {
private static final String TAG = "SessionActivity";
protected UiLifecycleHelper uiHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.splash);
}
public Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
protected void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
Toast.makeText(this, "Logged in...", Toast.LENGTH_LONG).show();
//finish();
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
Toast.makeText(this, "Logged out...", Toast.LENGTH_LONG).show();
}
}
@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
public class MyActivity extends SessionActivity {
private static final String TAG = "MyActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
}
<?xml version="1.0" encoding="utf-8"?>
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:layout_marginBottom="30dp" />
</LinearLayout>