按照此处给出的示例,我编写了一个Fragment
使用 Facebook Android SDK 3.5.2 管理 Facebook 登录/注销。
通过 Facebook 网络小部件的登录过程完成后,MainActivity
将再次启动(onCreate
被调用)。
然后在示例代码 if savedInstanceState!=null
in MainActivity
'sonCreate
中,savedInstanceState
检查是否FacebookPrefsFragment
必须恢复现有实例或是否必须创建新实例:
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);
}
我的问题是 myMainActivity
有很多Fragments
,并且savedInstanceState
不仅在 Facebook 登录过程中不能为空。所以我有两个问题:
为什么
MainActivity
在 Facebook 登录过程后再次创建?不应该只是FacebookPrefsFragment
'onActivityResult
被称为?如果要创建它,我怎么知道是因为 Facebook SDK 调用了它?
主要活动
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// If the Activity has been started after the Facebook login process, restart the FacebookPrefsFragment,
// but how can I be sure that MainActivity has been called by the Facebook app?
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment facebookPrefsFragment = new FacebookPrefsFragment();
fragmentTransaction.replace(R.id.content_container, facebookPrefsFragment, "facebookPrefsFragment");
fragmentTransaction.commit();
}
else{
// start other Fragment
}
}
// ...
}
FacebookPrefsFragment
public class FacebookPrefsFragment extends Fragment{
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(this.getClass().getSimpleName(), "Logged in", MyApplication.LOG_LEVEL);
} else if (state.isClosed()) {
Log.i(this.getClass().getSimpleName(), "Logged out", MyApplication.LOG_LEVEL);
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private UiLifecycleHelper uiHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_prefs, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setPublishPermissions(Arrays.asList("publish_actions"));
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);
}
}