片段在ondetach之后保持其状态

时间:2017-12-02 20:19:15

标签: android android-fragments

您好我需要一个片段来重置其状态(将所有 EditText 设置为空)但其状态会一直保存。 我在super.onCreate(null);中设置了onCreate,甚至试图在setText("")中设置所有 EditText 并且onDetach()被正确调用,我甚至尝试关闭使用getfragmentmanager().popbackstack()getFragmentManager().beginTransaction().remove(MyFragment.this).commit();

从其中分段

仍然没有,当替换时,片段的所有 EditText 仍然是以前的状态。

public class IngredienteComposerFragment extends Fragment {

private EditText mNameView;
private EditText mMinusPriceView;
private EditText mPlusPriceView;
private Button mSaveView;


public IngredienteComposerFragment() {
    // Required empty public constructor
}

public static IngredienteComposerFragment newInstance() {
    IngredienteComposerFragment fragment = new IngredienteComposerFragment();
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(null);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.fragment_ingrediente_composer, container, false);
    mNameView = v.findViewById(R.id.name);
    mMinusPriceView = v.findViewById(R.id.minus_price);
    mPlusPriceView = v.findViewById(R.id.plus_price);
    mSaveView = v.findViewById(R.id.save);
    return v;
}

@Override
public void onResume() {
    super.onResume();
    mSaveView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Ingrediente i = new Ingrediente(mNameView.getText().toString(), Double.parseDouble(mPlusPriceView.getText().toString()), Double.parseDouble(mMinusPriceView.getText().toString()));
            i.save();
//                getFragmentManager().popBackStack();
//                getFragmentManager().beginTransaction().remove(IngredienteComposerFragment.this).commit();
        }
    });
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
}

@Override
public void onDetach() {
    super.onDetach();
    mNameView.setText("");
    mMinusPriceView.setText("");
    mPlusPriceView.setText("");
    mSaveView.setOnClickListener(null);
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    EditText mNomeView = getView().findViewById(R.id.name);
    mNomeView.requestFocus();
    InputMethodManager imgr = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    imgr.showSoftInput(mNomeView, InputMethodManager.SHOW_IMPLICIT);
}

}

在活动中我首先在 FrameLayout 中加载片段:

getFragmentManager()
        .beginTransaction()
        .add(R.id.fragmentContainer, portataComposerFragment)
        .addToBackStack(null)
        .commit();

然后我用

加载我的片段
getFragmentManager().beginTransaction()
                    .replace(R.id.fragmentContainer, ingredienteComposerFragment)
                    .addToBackStack(null)
                    .commit();

经过一些测试后我注意到,因为我使用以下布局:

<android.support.design.widget.TextInputLayout
                android:id="@+id/name_wrapper"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Nome"
                    android:saveEnabled="false"
                    android:maxLines="1"
                    android:inputType="text"
                    />

对于我的edittext, saveEnabled =&#34; false&#34; 状态不会被保存,但我仍然想知道为什么它甚至可以保存当片段被破坏时(它调用onDetach方法)

0 个答案:

没有答案