Answer the question
In order to leave comments, you need to log in
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;
}
}
Answer the question
In order to leave comments, you need to log in
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();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question