简体   繁体   中英

Android Facebook: Login in One activity and Logout in another activity

From my App, I'm trying accessing facebook. The requirement will be like in two activity's I can log in to facebook and from only one activity I can log out. Consider Activity A and B, I can log in to facebook both from activity A and B and I can log out only from activity B after logout from activity B, if I try to access facebook from activity A it should ask for login. Right now it not asking from login, directly showing App authentication page 在此处输入图片说明

It is not logging out properly. Here is my code.

protected Handler mHandler = new Handler();
private final class FBLogoutListener implements RequestListener {
    @Override
    public void onComplete(String arg0, Object arg1) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Log.w(TAG,"onComplete:");
                FacebookUtility.clear();
            }
        });
    }
    @Override
    public void onFacebookError(FacebookError arg0, Object arg1) {}
    @Override
    public void onFileNotFoundException(FileNotFoundException arg0,
            Object arg1) {}
    @Override
    public void onIOException(IOException arg0, Object arg1) {}

    @Override
    public void onMalformedURLException(MalformedURLException arg0,
            Object arg1) {}
}

AsyncFacebookRunner facebookRunner = new AsyncFacebookRunner(FacebookUtility.mFB);
FBLogoutListener logoutListener = new FBLogoutListener();
facebookRunner.logout(this,logoutListener);

Any solution for this, where I'm doing mistakes.

If you are using Login manager class to login then this one line code is enough.

// Facebook logout
            LoginManager.getInstance().logOut();

This worked for me..

FB Logout in another activity :

// fb logout

    Session session = Session.getActiveSession();
                                if (session != null) {
                                    if (!session.isClosed()) {
                                        session.closeAndClearTokenInformation();
                                    }
                                } else {
                                    session = new Session(MainActivity.this);
                                    Session.setActiveSession(session);
                                    session.closeAndClearTokenInformation();
                                }

                                preferenceHelper.onLogOut();
                                finish();
                                startActivity(new Intent(MainActivity.this,
                                        LoginActivity.class));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM