I have a parent FragmentActivity with three child Tab Fragments. When a “Submit” button is pressed on the FragmentActivity, I want to sum the values of all the EditText fields in the different Tab Fragments. I think I have the right idea, using getSupportFragmentManager().findFragmentByTag(), however I’m finding that it is only returning values for the current active Fragment. I was under the assumption that I could access the inactive (or not visible) Tab Fragment’s as well:
int sum1 = getSupplyQuantities(Constants.TAB_TAG1);
int sum2 = getSupplyQuantities(Constants.TAB_TAG2);
int sum3 = getSupplyQuantities(Constants.TAB_TAG3);
int sum4 = getSupplyQuantities(Constants.TAB_TAG4);
private int getSupplyQuantities(String string) {
Activity tab = getSupportFragmentManager().findFragmentByTag(string)
.getActivity();
LinearLayout linearLayout = (LinearLayout) tab
.findViewById(R.id.get_supplies_layout);
int sum = 0;
for (int i = 0; i < linearLayout.getChildCount(); i++) {
View view = linearLayout.getChildAt(i);
QuantityTextView quantity = (QuantityTextView) view
.findViewById(R.id.get_supplies_list_quantity);
sum += Integer.parseInt(quantity.getText().toString());
}
return sum;
}
In the above scenario, sum1, sum2, sum3, and sum4 will all be the same. They will be the sum for the active Tab Fragment.
Does anyone have any ideas?
Thanks!
As Barak mentioned above, you need to get a hold of the fragment(s) that contain the view(s). Keep in mind that if you use some kind of a view pager, depending on how it’s configured, only the currently visible page/fragment is instantiated.
Solutions: