问题
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!
回答1:
Here is my solution, you make your MainActivity
as the first level, in this level your Android app will check whether the user is logged or not, based on this, go to the second level, which is either a Facebook login
page or the content slider
. For implementation, it would be like this:
This is the 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);
}
}
}
This is the 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...");
}
}
}
And here is the activity which you suppose to have a content slider, this will be displayed after the user has successfully logged in, here I call it ContentSliderActivity
:
public class ContentSliderActivity extends FragmentActivity{
.....................
}
回答2:
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.
来源:https://stackoverflow.com/questions/22119251/android-existing-fragment-conflicting-with-facebook-sdk