1

I want to add a Facebook login button to a Actionbar activity. I followed the instructions at https://developers.facebook.com/docs/android/login-with-facebook/v2.2?locale=de_DE and everything works just fine if I use a FragmentActivity. However if I extend the LoginActivity from ActionBarActivity although I don't get any errors the Login button is just not showing up in the activity.

Here is my code for activity_login.xml:

<com.facebook.widget.LoginButton
    android:id="@+id/authButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="30dp"
    />

For LoginActivity.java:

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class LoginActivity extends ActionBarActivity {

private MainFragment mainFragment;

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

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        mainFragment = new MainFragment();
        getSupportFragmentManager()
        .beginTransaction()
        .add(android.R.id.content, mainFragment)
        .commit();
    } else {
        // Or set the fragment from restored state info
        mainFragment = (MainFragment) getSupportFragmentManager()
        .findFragmentById(android.R.id.content);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.login, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

 public void onPause() {
     super.onPause();
     overridePendingTransition(0, 0);
 }
}

And MainFragment.java:

import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
import com.facebook.widget.LoginButton;

import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class MainFragment extends Fragment {

private static final String TAG = "MainFragment";
private UiLifecycleHelper uiHelper;

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

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, 
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_login, container, false);

    LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
    authButton.setFragment(this);

    return view;
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        Log.i(TAG, "Logged in...");
    } else if (state.isClosed()) {
        Log.i(TAG, "Logged out...");
    }
}

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

@Override
public void onResume() {
    super.onResume();
    uiHelper.onResume();
    //Add Monitor Facebook logins for statistical reasons!!
    // 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);
}
}
Axel
  • 1,415
  • 1
  • 16
  • 40
  • Is this issue happening in all android versions? And if you add normal `Button` instead of facebook login button, is it visible? – Abhishek V Jan 13 '15 at 05:12
  • @AbhishekV It seems to only happen in Android 5.0. It works fine in Android 4.4. I also don't see a button if I implement a normal android.widget.Button. – Axel Jan 13 '15 at 09:04
  • Check this : http://stackoverflow.com/questions/26633743/fragments-wont-show-up-in-activity-extending-actionbaractivity, https://code.google.com/p/android/issues/detail?id=78701. It seems to be the same problem – Abhishek V Jan 13 '15 at 09:54
  • @AbhishekV Please make your comment an answer to receive the open bounty. – Axel Jan 18 '15 at 14:06

2 Answers2

0

Implement the necessary callback functions and uiHelper stuff in your LoginActivity. If you're using a LoginButton from Facebook's package, just add Facebook's Login activity to your manifest.

<application>
...
    activity
        android:name="com.facebook.LoginActivity"
        android:label="@string/app_name" />
    <meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/app_id" />
</application>
Oscar
  • 3
  • 2
0

Refer these questions : Fragments won't show up in activity extending ActionBarActivity

https://code.google.com/p/android/issues/detail?id=78701

You can add fragment inside post method to fix this issue.

new Handler().post(new Runnable() {
    @Override
    public void run() {
        // Add the fragment on initial activity setup
    mainFragment = new MainFragment();
    getSupportFragmentManager()
    .beginTransaction()
    .add(android.R.id.content, mainFragment)
    .commit();
    }
});
Community
  • 1
  • 1
Abhishek V
  • 12,488
  • 6
  • 51
  • 63