da_st da_st - 8 days ago 4
Android Question

Hide facebook login button when authenticated

I have been following the facebook tutorial on how to implement the "login flow", i have gotten the login and logout to work fine.

But my issue is that i want the login button to disappear when the user has been authenticated, the button is shown in the MainActivity, but when the user is logged in i want it to be hidden, and instead show up in my SettingsActivity. This is the code i have:

public class MainFragment extends Fragment {

Firebase ref = new Firebase("https://test.firebaseio.com/");
SimpleLogin authClient = new SimpleLogin(ref, getActivity());

private UiLifecycleHelper uiHelper;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}

@Override
public void onResume() {
super.onResume();
// For scenarios where the main activity is launched and user
// session is not null, the session state change notification
// may not be triggered. Trigger it if it's open/closed.
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}

uiHelper.onResume();

}

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

@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}

@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}

@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.login_activity, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status"));
authButton.setFragment(this);

return view;
}

private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
authClient.loginWithFacebook("91023888187238", session.getAccessToken(), new SimpleLoginAuthenticatedHandler() {
public void authenticated(FirebaseSimpleLoginError error, FirebaseSimpleLoginUser user) {
if(error != null) {

// There was an error

}
else {


}
}
});
} else if (state.isClosed()) {
// Logged out of Facebook

authClient.logout();
}
}

}


I have tried adding this
authButton.setVisibility(View.GONE);
to the
if (state.isOpened()) {
but that caused error and the app crashed, any ideas how i could hide the loginbutton when the user is authenticated?

Answer

I solved it by saving the session token.

When I open my activity, ask by the token, if its there: check if it is OK. If its not or if no token was saved, then show login button.

When logging out, you should delete the token you saved! (i saved it in sharedPrefs)

Hope it helps!