为什么通过SupportFragmentManger添加片段的顺序很重要?

时间:2013-03-12 06:01:01

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

我正在创建一个AuthenticationActivity,它将为用户提供通过Facebook,Twitter或特定应用身份验证登录的选项。遵循写得好的tutorial Use Facebook Login from the Facebook Android SDK documentation后,我进行了Facebook身份验证。

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

经过四舍五入,我最终决定改变AuthenticationActivity通过SupportFragmentManager添加片段的顺序。当我做Facebook再次开始工作,但随后Twitter片段破裂。

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

这让我感觉更糟,而不是让我感觉更好。我承认没有完全理解片段,但我很困惑为什么添加片段的顺序会产生这种影响。首先出现的问题不起作用,但是第二个加入了第二个。值得注意的是,我正在使用及其片段实现。

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

AuthenticationActivity

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 documentation example

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

TwitterFragment

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问题添加上述片段的顺序?

1 个答案:

答案 0 :(得分:0)

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

您需要为每个片段分配不同的ID,否则您将始终为给定的ID获取相同的片段。我想你会得到第一个声明/添加的片段,但这取决于实现。 (您可能正在获得类强制转换异常)。

要解决此问题,请为它们分配不同的ID并将它们添加到不同的占位符。

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

这也需要其他更改,但希望你能到达那里。