安卓:在整个传递活动中的Facebook会话 [英] Android: Passing a Facebook session across activities

查看:126
本文介绍了安卓:在整个传递活动中的Facebook会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待通过一个Facebook会话各种活动。我看到的例子来自Facebook的SDK,有人提到,简单的例子有办法做到这一点: <$c$c>https://github.com/facebook/facebook-android-sdk/blob/master/examples/simple/src/com/facebook/android/SessionStore.java

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.com/facebook/facebook-android-sdk/blob/master/examples/simple/src/com/facebook/android/SessionStore.java

但如何工作的呢?在我的 MainActivity ,我有这样的:

But how does this work? In my MainActivity, I have this:

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() {
        }
    });
}

但我怎么把这篇文章也给我的光催化活性的活动?有没有这个正在实施的例子吗?

But how do I pass this along to my PhotoActivity activity? Is there an example of this being implemented?

推荐答案

使用共享preferences在整个活动传递的数据是不是好主意。共享preferences使用一些数据存储到跨应用程序重新启动或设备重新启动的内存。

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.

相反,你有两个选择:

  1. 声明一个静态变量来保存Facebook的会议,这是最简单的方法,但我不会建议使用静态字段就没有其他的办法。

  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.

请一个类实现parcelable,并设置你的Facebook的对象那里,看到一个parcelable实现如下:

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 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();
    }
}

这篇关于安卓:在整个传递活动中的Facebook会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆