I
I
IvanOne2017-01-05 08:45:18
Android
IvanOne, 2017-01-05 08:45:18

How to properly organize back stack fragments?

Hello everyone, help a newbie to understand the fragments. I have a List Fragment and a Form Fragment to Add an Item to a List. I want to make it so that the back button is only on the form fragment and leads to the list.
here are the snippet codes:

public class XeListFragment extends ListFragment {

    private FragmentManager fragmentManager;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_xe_list, container, false);
        FloatingActionButton fabCreate = (FloatingActionButton) view.findViewById(R.id.createItem);
        fragmentManager = getActivity().getSupportFragmentManager();
        fabCreate.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                AddItemFragment addFragment = new AddItemFragment();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.container, addFragment);

                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        CalcActivity activity = (CalcActivity) getActivity();
        activity.navigationView.setCheckedItem(R.id.nav_xe_list);
        ArrayList<XeData> list = activity.getXeData();
        XeAdapter adapter = new XeAdapter(activity, list);
        setListAdapter(adapter);
        if(!adapter.isEmpty()){
            ListView lv = getListView();

            View lvFooter = ((LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE))
                    .inflate(R.layout.lv_footer, null, false);
            String total_text = "Итого: " + adapter.getXeResult() + " XE";
            ((TextView) lvFooter.findViewById(R.id.total_text)).setText(total_text);
            lv.setDivider(null);
            lv.addFooterView(lvFooter);
        }

    }

}

public class AddItemFragment extends Fragment{

    EditText carbCount;
    EditText foodCount;
    Button setResultButton;

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


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_add_item, container, false);
        CalcActivity activity = (CalcActivity) getActivity();
        activity.navigationView.setCheckedItem(R.id.nav_xe_list);
        carbCount = (EditText) view.findViewById(R.id.carbCount);
        foodCount = (EditText) view.findViewById(R.id.foodCount);
        setResultButton = (Button) view.findViewById(R.id.xeAdd);
        setResultButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (ValidationEditText.validate(carbCount) && ValidationEditText.validate(foodCount)){
                    double carbCountUnit = Double.valueOf(carbCount.getText().toString());
                    double foodCountUnit = Double.valueOf(foodCount.getText().toString());
                    double result = ((carbCountUnit/12)/100) * foodCountUnit;
                    CalcActivity activity = (CalcActivity) getActivity();
                    ArrayList<XeData> list = activity.getXeData();
                    list.add(new XeData(carbCountUnit, result));
                    final InputMethodManager imm = (InputMethodManager) getActivity()
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getView().getWindowToken(), 0);
                    activity.setXeData(list);
                    XeListFragment xe_list = new XeListFragment();
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.container, xe_list);
                    fragmentTransaction.commit();
                }
            }
        });
        return view;
    }
}

In this scenario, after adding an element, I return to the list and there the back button leads to the same list, and the fragment is called twice, what should I do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2017-01-05
@zagayevskiy

Error in setResultButton handler

XeListFragment xe_list = new XeListFragment();
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(R.id.container, xe_list);
                    fragmentTransaction.commit();

You have a previous transaction in the backstack. You should instead do fragmentManager.popBackStack(); - pull the transaction out of the backstack and thereby return to the previous screen.
But it's generally bad that the append fragment knows that there are other fragments and manipulates the backstack. It is better to organize this, for example, through an activity.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question