Facebook用户会话数据未在需要之前设置

时间:2013-12-06 05:21:54

标签: java android facebook-graph-api

我创建了一个处理我的fb会话的类,它被所有其他活动扩展。

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

但是当我的活动运行它时,它会在设置之前处理onCreate()中的所有内容,因此它总是会失败。

public class MyActivity extends SessionActivity {

    private static final String TAG = "ShindigActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shindig);

        if(userDetails.getId() != null) {
            Log.i(TAG, "fb id is set: "+userDetails.getId());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.shindig, menu);
        return true;
    }
}

应如何处理?

0 个答案:

没有答案