2

I made facebook login integration using LoginButton in my android app. now when i am logged in then my login button automatically changes to logout. I dont want that as i got another activity to logout. how should i prevent this. Following is my code.

public class FacebookLogin extends AppCompatActivity {

CallbackManager callbackManager;
RelativeLayout relativeLayout;
Bundle bundle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    facebookSDKInitialize();
    setContentView(R.layout.activity_facebook_login);
    relativeLayout =(RelativeLayout)findViewById(R.id.relative_layout);

    bundle = new Bundle();

    if (AppStatus.getInstance(this).isOnline()) {

        LoginButton loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setTextSize(16);
        loginButton.setReadPermissions("email");
        getLoginDetails(loginButton);



    } else {


        Snackbar snackbar = Snackbar
                .make(relativeLayout, "No internet connection!", Snackbar.LENGTH_INDEFINITE)
                .setAction("RETRY", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(FacebookLogin.this, FacebookLogin.class);
                        startActivity(intent);

                    }
                });

        // Changing message text color
        snackbar.setActionTextColor(Color.RED);

        // Changing action button text color
        View sbView = snackbar.getView();
        TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
        textView.setTextColor(Color.YELLOW);
        snackbar.show();
    }



}

/*
Initialize the facebook sdk.
And then callback manager will handle the login responses.
 */
protected void facebookSDKInitialize() {

    FacebookSdk.sdkInitialize(getApplicationContext());
    callbackManager = CallbackManager.Factory.create();
}

/*
Register a callback function with LoginButton to respond to the login result.
*/

protected void getLoginDetails(LoginButton login_button){

    // Callback registration
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult login_result) {
            getUserInfo(login_result);

        }

        @Override
        public void onCancel() {
            // code for cancellation
        }

        @Override
        public void onError(FacebookException exception) {
            //  code to handle error
        }
    });
}

/*
To get the facebook user's own profile information via  creating a new request.
When the request is completed, a callback is called to handle the success condition.
*/
protected void getUserInfo(LoginResult login_result){

    GraphRequest data_request = GraphRequest.newMeRequest(
            login_result.getAccessToken(),
            new GraphRequest.GraphJSONObjectCallback(){
                @Override
                public void onCompleted(
                        JSONObject json_object,
                        GraphResponse response) {

                    try {
                        final String name =json_object.getString("name");
                        final String email =json_object.getString("email");
                        bundle.putString("userName",name);
                        bundle.putString("userEmail",email);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    Intent intent = new Intent(FacebookLogin.this,HomePage.class);
                    intent.putExtras(bundle);
                    intent.putExtra("jsondata",json_object.toString());
                    startActivity(intent);
                    finish();
                }
            });
    Bundle permission_param = new Bundle();
    permission_param.putString("fields", "id,name,email,picture.width(120).height(120)");
    data_request.setParameters(permission_param);
    data_request.executeAsync();

}

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

    Log.e("data", data.toString());
}

@Override
protected void onResume() {
    super.onResume();

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}

@Override
protected void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}
}
Soumya Rauth
  • 1,163
  • 5
  • 16
  • 32
  • 1
    customise fb button, set fb:logout_text="" http://stackoverflow.com/questions/16314651/customize-android-facebook-login-button?rq=1 – SHIVANI GARG Dec 19 '16 at 09:00

1 Answers1

3

There is a very simple solution for that, just hide the facebook login button by setting its visibility to gone in the xml

then make your own button and setOnclickListener on it then inside your button , I am supposing your facebook button name is btnFacebook so write this inside of onclicklistener of your button btnFacebook.performClick();

and don't forget to use this in your logout button

LoginManager.getInstance().logOut();
Umar Ata
  • 4,170
  • 3
  • 23
  • 35
  • Didn't work for me except the fact that I could just login with a new custom button. – Soumya Rauth Dec 20 '16 at 06:41
  • The thing that din work was when I logged in, the login screen should lead to the main activity of my app. Rather it goes to the logout activity(Seemingly the login button turns into logout).-- Thanks. – Soumya Rauth Dec 20 '16 at 08:15
  • you should make three screens, one is splash in which you need to check in sharedpreferences islogin then goto mainactivity, else goto login activity and if login successful then store some info in sharedpreference then goto mainactivity , so it will not be seen again unless you logout – Umar Ata Dec 20 '16 at 08:20