1

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.

DMT82
  • 871
  • 2
  • 14
  • 32

1 Answers1

0

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

JohanShogun
  • 2,956
  • 21
  • 30
  • I googled LocalBroadcastManager, not so much good tutorials and examples did come up. Is it someone outthere that know how to hide the logout button to facebook after you logged in? Or put it in the navigatordrawer and so on. – DMT82 Jul 26 '15 at 12:02
  • This describes how to do it, http://stackoverflow.com/questions/8802157/how-to-use-localbroadcastmanager, second hit on google. – JohanShogun Jul 26 '15 at 12:12
  • Tried that example in my code, didnt work. I have a Fragment where the code for the button are and a Activity. I followed this tutorial to make the login, and I cant get it to work with LocalBroadcastManager. Will see if I can find someone that made this with Facebook SDK and made the logout button hide or put it in navigatordrawer or something after login. Thanks. – DMT82 Jul 26 '15 at 14:32