da_st da_st - 1 year ago 168
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;

public void onCreate(Bundle savedInstanceState) {
uiHelper = new UiLifecycleHelper(getActivity(), callback);

public void 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);



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

public void onPause() {

public void onDestroy() {

public void onSaveInstanceState(Bundle outState) {

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"));

return view;

private Session.StatusCallback callback = new Session.StatusCallback() {
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



I have tried adding this
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 Source

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!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download