2012-05-11 93 views
3

我期待跨活動傳遞Facebook會話。我從Facebook的SDK中看到了這個例子,並且有人提到「簡單」示例有一種方法可以做到這一點:https://github.com/facebook/facebook-android-sdk/blob/master/examples/simple/src/com/facebook/android/SessionStore.javaAndroid:通過活動傳遞Facebook會話

但是這是如何工作的?在我MainActivity,我有這樣的:

mPrefs = getPreferences(MODE_PRIVATE); 
String accessToken = mPrefs.getString("access_token", null); 
long expires = mPrefs.getLong("access_expires", 0); 
if (accessToken != null) { 
    //We have a valid session! Yay! 
    facebook.setAccessToken(accessToken); 
} 
if (expires != 0) { 
    //Since we're not expired, we can set the expiration time. 
    facebook.setAccessExpires(expires); 
} 

//Are we good to go? If not, call the authentication menu. 
if (!facebook.isSessionValid()) { 
    facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() { 
     @Override 
     public void onComplete(Bundle values) { 
     } 

     @Override 
     public void onFacebookError(FacebookError error) { 
     } 

     @Override 
     public void onError(DialogError e) { 
     } 

     @Override 
     public void onCancel() { 
     } 
    }); 
} 

但我怎麼把這篇文章也給我PhotoActivity活動?有沒有一個正在實施的例子?

回答

4

使用SharedPreferences在活動間傳遞數據不是好主意。 SharedPreferences用於在應用程序重新啓動或設備重新啓動時將一些數據存儲到內存中。

相反,你有兩個選擇:

  1. 聲明一個靜態變量來持有Facebook的會議,這是最簡單的方法,但我不會建議使用靜態字段就沒有其他的辦法。

  2. 做一個類實現parcelable,並設置你的Facebook對象那裏,看到一個parcelable執行如下命令:

    // simple class that just has one member property as an example 
    public class MyParcelable implements Parcelable { 
        private int mData; 
    
        /* everything below here is for implementing Parcelable */ 
    
        // 99.9% of the time you can just ignore this 
        public int describeContents() { 
         return 0; 
        } 
    
        // write your object's data to the passed-in Parcel 
        public void writeToParcel(Parcel out, int flags) { 
         out.writeInt(mData); 
        } 
    
        // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods 
        public static final Parcelable.Creator<MyParcelable> CREATOR = new Parcelable.Creator<MyParcelable>() { 
         public MyParcelable createFromParcel(Parcel in) { 
          return new MyParcelable(in); 
         } 
    
         public MyParcelable[] newArray(int size) { 
          return new MyParcelable[size]; 
         } 
        }; 
    
        // example constructor that takes a Parcel and gives you an object populated with it's values 
        private MyParcelable(Parcel in) { 
         mData = in.readInt(); 
        } 
    } 
    
1

這個例子非常有整個實現。您只需使用SharedPreferences來存儲您的會話。當您在PhotoActivity中需要它時,只需再次查看SharedPreferences(也許通過您的SessionStore靜態方法,如果您遵循相同的模式)以獲取之前存儲的Facebook會話。

+0

因此SharedPreferences在整個活動中傳輸數據? –

+0

是的。 'SharedPreferences'是共享的。 :)請參閱此[doc](http://developer.android.com/guide/topics/data/data-storage.html#pref)以獲取更多信息。 – kabuko

+0

使用SharedPreferences在活動之間傳遞數據不是一個好主意。 – jeet

2

對於FB SDK 3.5,在我的FB登錄活動,我通過積極通過額外的意圖會話對象,因爲該會話類實現Serializable接口

private void onSessionStateChange(Session session, SessionState state, Exception exception) { 
    if (exception instanceof FacebookOperationCanceledException || exception instanceof FacebookAuthorizationException) { 
     new AlertDialog.Builder(this).setTitle(R.string.cancelled).setMessage(R.string.permission_not_granted).setPositiveButton(R.string.ok, null).show(); 

    } else { 

     Session session = Session.getActiveSession(); 

     if ((session != null && session.isOpened())) { 
      // Kill login activity and go back to main 
      finish(); 
      Intent intent = new Intent(getApplicationContext(), MainActivity.class); 
      intent.putExtra("fb_session", session); 
      startActivity(intent); 
     } 
    } 
} 

從我的MainActivity的onCreate(),我檢查額外的意圖和啓動會話:

Bundle extras = getIntent().getExtras(); 
if (extras != null) {   
    Session.setActiveSession((Session) extras.getSerializable("fb_session")); 
} 
+0

請你可以看看我的問題在這裏http://stackoverflow.com/questions/18676582/how-to-use-logged-in-session-when-passing-from-one-activity-to-another,因爲我試圖實施您的解決方案,但我掙扎。謝謝 – Hishalv

+0

這絕對是facebook SDK 3.5的正確選擇 – jamesc