Android - 现有片段与Facebook SDK冲突?

时间:2014-03-01 19:30:05

标签: java android facebook android-fragments facebook-sdk-3.0

我正在开发一款Android应用程序,我希望将Facebook登录集成到主页面。我遵循这些指示:

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

部分演练列表更改MainActivity类以扩展FragmentActivity。但是,我的MainActivity类已经扩展了它,因为我在主视图上实现了内容滑块。 MainActivity的原始onCreate代码是:

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

这很有效。在Facebook登录演练之后,我按照字面意思添加了一个新的“MainFragment”类:

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

,还在我的MainActivity布局中添加了一个com.facebook.widget.LoginButton(这似乎显示正常),并尝试通过将以下内容添加到MainActivity类的OnCreate部分(如上所示,但是为简洁而不再重复这一切):

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

我从根本上解决的问题是如何使我现有的滑块和Facebook登录很好地一起玩?如果我添加OnCreate of MainActivity所需的Facebook代码,那么我的滑块就会消失。如果我不添加代码,那么Facebook登录显然不起作用。我已经尽可能多地阅读片段,认为我可能在那里做错了什么,但没有多少运气......

提前感谢您提供的任何帮助!

2 个答案:

答案 0 :(得分:0)

以下是我的解决方案,您将MainActivity作为第一级,在此级别,您的Android应用将检查用户是否已登录,基于此,转到第二级,即Facebook login页面或content slider。为了实现,它将是这样的:

这是MainActivity

    public class MainActivity extends FragmentActivity{
        private LoginFragment loginFragment;

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

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


    }

这是LoginFragment

public class LoginFragment extends Fragment {

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

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

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.login_fragment, 
            container, false);        
    authButton=(LoginButton)view.findViewById(R.id.login_button);
    authButton.setFragment(this);
    return view;
}

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

@Override
public void onResume() {
    super.onResume();
    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);
}

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

}

以下是您希望有一个内容滑块的活动,这将在用户成功登录后显示,此处我称之为ContentSliderActivity

    public class ContentSliderActivity extends FragmentActivity{
      .....................
    }

答案 1 :(得分:0)

我能够在CreateView方法的MainFragment中识别出问题。出于某种原因,如果我删除了这个覆盖,我没有问题我的内容滑块被删除。不知道为什么,而不是添加以下行:LoginButton authButton =(LoginButton)findViewById(R.id.authButton); authButton.setFragment(本);在MainFragment的onViewCreate方法中,我只是将它们移动到MainActivity中的onCreate方法。似乎对我有用,虽然不完全确定原始问题是诚实的。