3

我正在创建一个AuthenticationActivity它将为用户提供通过 Facebook、Twitter 或应用程序特定身份验证登录的选项。在遵循Facebook Android SDK 文档中编写良好的教程Use Facebook Login之后,我的 Facebook 身份验证工作。

不幸的是,当我继续添加 Twitter 片段以提供类似的登录功能时,我遇到了问题。突然,Facebook 登录按钮不会改变状态,我对 Facebook 的身份验证也不会完全完成,尽管我可以看到身份验证回调正在返回。

兜兜转转后,我终于决定更改AuthenticationActivity通过SupportFragmentManager. 当我这样做时,Facebook 又开始工作了,但后来 Twitter 片段坏了。

getSupportFragmentManager()
    .beginTransaction()
    .add(android.R.id.content, twitterFragment)
    .add(android.R.id.content, facebookFragment)
    .commit();

而不是让我感觉更好,这让我感觉更糟。我承认没有完全理解片段,但我完全不明白为什么添加片段的顺序会产生这种影响。先到先得是行不通的,但是后加第二个就行了。值得注意的是,我正在使用及其片段实现。

您将在下面找到我的活动和片段代码

身份验证活动

public class AuthenticationActivity extends SherlockFragmentActivity {

    private FacebookFragment facebookFragment;
    private TwitterFragment twitterFragment;

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

        if (savedInstanceState == null) {
            addFragments();
        } else {
            restoreFragments();
        }
    }

    private void addFragments() {
        facebookFragment = new FacebookFragment();
        twitterFragment = new TwitterFragment();

        // TODO the order of this MATTERS if Facebook isn't last
        // Facebook auth breaks and vice versa.
        getSupportFragmentManager()
            .beginTransaction()
            .add(android.R.id.content, twitterFragment)
            .add(android.R.id.content, facebookFragment)
            .commit();
    }

    private void restoreFragments() {
        facebookFragment = (FacebookFragment) getSupportFragmentManager()
            .findFragmentById(android.R.id.content);
        twitterFragment = (TwitterFragment) getSupportFragmentManager()
            .findFragmentById(android.R.id.content);
    }
}

Facebook Fragment(主要基于SDK 文档示例

public class FacebookFragment extends SherlockFragment {

    private UiLifecycleHelper uiHelper;
    private static final String TAG = FacebookFragment.class.getSimpleName();

    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.authentication, container, false);

        LoginButton facebookButton
            = (LoginButton) view.findViewById(R.id.facebookButton);
        facebookButton.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...");
        } else if (state.isClosed()) {
            Log.i(TAG, "Logged out...");
        } else {
            Log.i(TAG, "unknown state " + state);
        }
    }
}

推特片段

public class TwitterFragment extends SherlockFragment {

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

        Button twitterButton = (Button) view.findViewById(R.id.twitterButton);
        twitterButton.setOnClickListener(twitterClickListener);

        return view;
    }

    private OnClickListener twitterClickListener = new OnClickListener() {
        public void onClick(View v) {
            Context context = v.getContext();

            Intent intent = new Intent(context, TwitterLoginActivity.class);
            context.startActivity(intent);
        }
    };
}

为什么上面的片段是按顺序添加的SupportFragmentManger呢?

4

1 回答 1

0
 private void restoreFragments() {
        facebookFragment = (FacebookFragment) getSupportFragmentManager()
            .findFragmentById(android.R.id.content);
        twitterFragment = (TwitterFragment) getSupportFragmentManager()
            .findFragmentById(android.R.id.content);
    }

You need to assign each fragment a different id otherwise you will always get the same fragment for a given id. I guess you'd get the first declared/added fragment but that depends in the implementation. (You probably are getting a class cast exception).

To fix this, assign them different ids and add them to different placeholders.

.beginTransaction()
            .add(R.id.twitter_fragment, twitterFragment)
            .add(R.id.facebook_fragment, facebookFragment)
            .commit();

This requires other changes as well but hopefully you can get there.

于 2013-03-27T14:45:35.833 回答