切换方向时为什么片段会改变?

时间:2014-01-11 00:23:47

标签: android android-fragments orientation

所以基本上发生的事情是我的LoginFragment应该在单击按钮时加载创建帐户片段,但是当方向更改为横向时,它会加载LoginFragment的横向。如果将其切换回肖像,则会崩溃。下面是在LoginFragment中按下注册按钮时加载createAccountFragment的代码。如果您需要任何其他代码,请告诉我。谢谢,我非常感谢你们!注意:如果我销毁了登录片段视图或其他内容会有帮助吗?

/**
 * Load the create account view.
 */
private void createAccount() {
    FragmentManager fragmentManager = getActivity()
            .getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager
            .beginTransaction();
    fragmentTransaction
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.replace(
            ((ViewGroup) getView().getParent()).getId(),
            CreateAccountFragment.newInstance());
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
}

onCreate for LoginFragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_login, container, false);
    mUserNameEditText = (EditText) view.findViewById(R.id.username);

    mPasswordEditText = (EditText) view.findViewById(R.id.password);
    mPasswordEditText
            .setOnEditorActionListener(new TextView.OnEditorActionListener() {
                @Override
                public boolean onEditorAction(TextView textView, int id,
                        KeyEvent keyEvent) {
                    if (id == EditorInfo.IME_NULL) {
                        attemptLogin();
                        return true;
                    }
                    return false;
                }
            });

    view.findViewById(R.id.sign_in_button).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    attemptLogin();
                }
            });

    view.findViewById(R.id.register_button).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    createAccount();
                }
            });

    view.findViewById(R.id.forgot_button).setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    forgotPassword();
                }
            });
    return view;
}

On create for CreateAccountFragment
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

                View view = inflater.inflate(R.layout.fragment_create_account, container, false);

                mUserNameEditText = (EditText)view.findViewById(R.id.etUsername);
                mEmailEditText = (EditText)view.findViewById(R.id.etEmail);
                mPasswordEditText = (EditText)view.findViewById(R.id.etPassword);
                mConfirmPasswordEditText = (EditText)view.findViewById(R.id.etPasswordConfirm);

                mCreateAccountButton = (Button)view.findViewById(R.id.btnCreateAccount);
                mCreateAccountButton.setOnClickListener(this);
                return view;
        }

登录片段的onsaveinstance代码

    @Override
    public void onSaveInstanceState(Bundle outState) {  
        outState.putString(EXTRA_USERNAME, mUserNameEditText.getText()
                .toString());
        outState.putString(EXTRA_PASSWORD, mPasswordEditText.getText()
                .toString());
        super.onSaveInstanceState(outState);
    }
onsaveinstance code for create account fragment
 @Override
        public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);

                if(savedInstanceState != null){
                        mEmailEditText.setText(savedInstanceState.getString(EXTRA_EMAIL));
                        mUserNameEditText.setText(savedInstanceState.getString(EXTRA_USERNAME));
                        mPasswordEditText.setText(savedInstanceState.getString(EXTRA_PASSWORD));
                        mConfirmPasswordEditText.setText(savedInstanceState.getString(EXTRA_CONFIRM));
                }
        }

        @Override
        public void onSaveInstanceState(Bundle outState) {
                super.onSaveInstanceState(outState);
                outState.putString(EXTRA_EMAIL, mEmailEditText.getText().toString());
                outState.putString(EXTRA_USERNAME, mUserNameEditText.getText().toString());
                outState.putString(EXTRA_PASSWORD, mPasswordEditText.getText().toString());
                outState.putString(EXTRA_CONFIRM, mConfirmPasswordEditText.getText().toString());
        }

1 个答案:

答案 0 :(得分:1)