Skip to content Skip to sidebar Skip to footer

Android: Passing A Facebook Session Across Activities

I'm looking to pass a Facebook session across activities. I saw the example from Facebook's SDK and someone mentioned that the 'Simple' example has a way to do this: https://github

Solution 1:

Using SharedPreferences to pass data across activities is not good idea. SharedPreferences used to store some data into memory across application restart or device re-boot.

Instead you have two options:

  1. Declare a static variable to hold facebook session, which is simplest method, but I wont recommend to use Static Fields as far there is no other way.

  2. Make an class implementing parcelable, and set your facebook object there, see an parcelable implementation as follows:

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

Solution 2:

For FB SDK 3.5, In my FB login activity, I pass the active session object via intent extras because the Session class implements serializable:

privatevoid onSessionStateChange(Session session, SessionState state, Exceptionexception) {
    if (exceptioninstanceof FacebookOperationCanceledException || exceptioninstanceof 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);
        }
    }
}

From my MainActivity onCreate(), I check for intent extra and initiate the session:

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

Solution 3:

The example pretty much has the whole implementation. You just use SharedPreferences to store your session. When you need it in your PhotoActivity, just look in SharedPreferences again (perhaps via your SessionStore static methods if you followed the same pattern) to get the Facebook Session that you previously stored.

Post a Comment for "Android: Passing A Facebook Session Across Activities"