简体   繁体   中英

Facebook SDK 4.4.+ | How do I hide logout button after login?

How do I hide the login button after that I have logged in to Facebook in my app? I want to put my login(logout) button in menu_login.xml or another place.

my LoginActivity.

public class LoginActivity extends AppCompatActivity {

private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();

    setContentView(R.layout.activity_login);

    loginButton = (LoginButton) findViewById(R.id.login_button);

    // Callback registration
    loginButton.registerCallback(callbackManager, new     FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            Intent intent = new Intent (LoginActivity.this,     MainActivity.class);
            startActivity(intent);


        }

        @Override
        public void onCancel() {

        }

        @Override
        public void onError(FacebookException exception) {

        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent         data) {
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);
}
}

This is what I put in the menu_login.xml

    <item android:id="@+id/login_button" android:title="Log out"
    android:orderInCategory="10" app:showAsAction="never" />

But when I press the log out button, nothing will happen.

I have also tried this to send me from one to another Activity after the login, but then more problems comes up.

 @Override
        public void onSuccess(LoginResult loginResult) {
            Intent intent = new Intent (LoginActivity.this, MainActivity.class);
            startActivity(intent);
  1. I cant put the login/out button in the new Activity.
  2. When I press the login, it will jump fast to the logout button site and then to the new Activity, and thats weird, so I dont want to send from one to another Activity.

So, how do I hide the logout button after login in?

Thanks.

You could use the local broadcast manager class and broadcast in your application that you have successfully logged into Facebook. Your MainActivity (or whoever is interested) would then need to listen to that broadcast and act accordingly.

More info: http://developer.android.com/intl/es/reference/android/support/v4/content/LocalBroadcastManager.html

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