Search code examples
javaandroidfacebookandroid-fragmentsfacebook-sdk-3.0

Android - Existing Fragment Conflicting with Facebook SDK?


I'm developing an Android app and am to the point where I would like to integrate Facebook login onto the main page. I am following these directions for the same:

https://developers.facebook.com/docs/android/login-with-facebook/#step1

Part of the walkthrough lists changing the MainActivity class to extend FragmentActivity. My MainActivity class, however, already extended it because of a content slider I implemented on the main view. The original onCreate code of MainActivity was:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Instantiate a ViewPager and a PagerAdapter.
    mPager = (ViewPager) findViewById(R.id.pager);
    mPagerAdapter = new ScreenSlidePagerAdapter(getFragmentManager());
    mPager.setAdapter(mPagerAdapter);
    mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
             invalidateOptionsMenu();
        }
    });
}

And this was working great. Following the Facebook login walkthrough, I added a new 'MainFragment' class following the directions as literally as possible:

public class MainFragment extends Fragment {

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

    return view;
    }
}

, also added a com.facebook.widget.LoginButton to my MainActivity layout (this seems to display fine), and tried to tie it all together by adding the following to the OnCreate section of the MainActivity class (displayed above, but not repeating it all here again for brevity):

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

The question I fundamentally have is how to make both my existing slider and Facebook login play together nicely? If I add the Facebook code needed to OnCreate of MainActivity, then my slider goes away. If I don't add the code, then Facebook login obviously doesn't work. I've read as much as possible on Fragments thinking I might be doing something wrong there, but haven't had much luck...

Thanks in advance for any help you can provide!


Solution

  • I was able to discern the problem was in the MainFragment on CreateView method. For some reason, if I removed this override, I didn't have a problem where my content sliderr gets removed. No idea why, but instead of adding the following lines: LoginButton authButton = (LoginButton) findViewById(R.id.authButton); authButton.setFragment(this); in the onViewCreate method of MainFragment, I simply moved them to the onCreate method in MainActivity. Seems to have worked for me, although not entirely sure what the original problem was honestly.