因此,基本上发生了什么事,我的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();
}


LoginFragment的onCreate

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

最佳答案

根据方向变化重新创建活动,从而有效地使其成为一项新活动。 You need to override onSaveInstanceState(Bundle savedInstanceState) and write the application state values you want to change to the Bundle parameter

关于android - 为什么在切换方向时 fragment 会变回来?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21056894/

10-12 04:23