0

按照此处给出的示例,我编写了一个Fragment使用 Facebook Android SDK 3.5.2 管理 Facebook 登录/注销。

通过 Facebook 网络小部件的登录过程完成后,MainActivity将再次启动(onCreate被调用)。

然后在示例代码 if savedInstanceState!=nullin 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);
    }
}
4

3 回答 3

2

这篇文章有点过时了,但我想我会把它留在这里,以防有人遇到我面临的问题。当未安装本机 Facebook 应用程序时,我的应用程序正在重新创建,并且即使我有正确的请求代码和数据,callbackManager.onActivityResult() 仍然返回 false。如果初始调用返回 false,我可以通过重置和调用以下内容来解决此问题:

facebookButton.registerCallback(callbackManager, Activity.this);
callbackManager.onActivityResult(requestCode, resultCode, data);
于 2017-05-31T16:08:58.840 回答
1

你需要玩弄

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

查看应用程序来自时的请求代码是什么。来源:https ://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/widget/FacebookDialog.java#L210

几个月前我对其进行了测试,您可以使用 requestCode 来确定应用程序是否从 Facebook 回来。

[编辑]

这是一个丑陋的黑客,但如果requestCode == 64206那时你的活动刚刚从 Facebook 回来。

于 2013-10-04T07:13:01.130 回答
0

糟糕,我发现了为什么MainActivity每次都会重新创建:那是因为我在手机上激活了“不保留活动”选项!现在我必须找出在这种情况下该怎么办......

于 2013-10-04T10:26:16.020 回答