Isaac Kennedy Isaac Kennedy - 1 month ago 12
Android Question

Same fragment XML with different TAGs multiple times in Android within a TabLayout

the problem is that I am calling a Fragment class multiple times (everyone with a different argument), and then I need to recall afterwards this arguments to do some processing. But, whenever I call this argument when I need it, it is the same for every fragment, even though I explicitly called it with different arguments!

Here's some code to help you guys understand the problem.

NewStudentActivity:

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_student);

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

viewPager = (ViewPager) findViewById(R.id.viewpager);
tabLayout = (TabLayout) findViewById(R.id.tabs);

fab = (FloatingActionButton) findViewById(R.id.fab_new_student);

UserDataFragment frag = new UserDataFragment();
frag.show(getSupportFragmentManager(), "user_data");

measurementList = new SparseArray<>(4);

//...

private void setupViewPager (ViewPager viewPager){
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());

if (calcMethod == Utility.PETROVSKI){
if (userGender == Utility.GENDER_MALE){


adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_TRICIPITAL),
Utility.MEASUREMENT_TRICIPITAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_PANTURRILHA),
Utility.MEASUREMENT_PANTURRILHA);

} else if (userGender == Utility.GENDER_FEMALE) {
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_AXILAR),
Utility.MEASUREMENT_AXILAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_COXA),
Utility.MEASUREMENT_COXA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_PANTURRILHA),
Utility.MEASUREMENT_PANTURRILHA);

} else {
finish();
}
} else if (calcMethod == Utility.GUEDES) {
if (userGender == Utility.GENDER_MALE){
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_ABDOMINAL),
Utility.MEASUREMENT_ABDOMINAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);

} else if (userGender == Utility.GENDER_FEMALE) {
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_COXA_PROXIMAL),
Utility.MEASUREMENT_COXA_PROXIMAL);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUPRAILIACA),
Utility.MEASUREMENT_SUPRAILIACA);
adapter.addFragment(MeasurementFragment.newInstance(Utility.MEASUREMENT_SUBESCAPULAR),
Utility.MEASUREMENT_SUBESCAPULAR);

} else {
finish();
}
} else {
finish();
}

viewPager.setAdapter(adapter);
}


ViewPagerAdapter:

class ViewPagerAdapter extends FragmentPagerAdapter{
private final List<Fragment> fragmentList = new ArrayList<>();
private final List<String> fragmentTitleList = new ArrayList<>();

public ViewPagerAdapter(FragmentManager manager){
super(manager);
}

@Override
public Fragment getItem(int position){
return fragmentList.get(position);
}

@Override
public int getCount(){
return fragmentList.size();
}

public void addFragment (Fragment fragment, String title){
fragmentList.add(fragment);
fragmentTitleList.add(title);
}

@Override
public CharSequence getPageTitle(int position) {
return fragmentTitleList.get(position);
}

}


MeasurementFragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
view = inflater.inflate(R.layout.fragment_measurement, container, false);

measurement1 = (EditText) view.findViewById(R.id.te_first_measurement);
measurement2 = (EditText) view.findViewById(R.id.te_second_measurement);
measurement3 = (EditText) view.findViewById(R.id.te_third_measurement);
median = (EditText) view.findViewById(R.id.mediana);
median.setKeyListener(null);

btn_check = (AppCompatButton) view.findViewById(R.id.btn_check_measurements);

btn_check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
returnValues();
}
});

return view;
}

public static MeasurementFragment newInstance(String measurement){
MeasurementFragment frag = new MeasurementFragment();
Bundle args = new Bundle();

arg = measurement;

args.putString(ARG_MEASUREMENT, measurement);

frag.setArguments(args);
return frag;
}

private void returnValues(){
if (checkMedian()){
Bundle data = new Bundle();

receiver = (MeasurementFinished) getActivity();

data.putString("measurement", arg);
data.putDouble("mediana", median_value);
data.putDouble("measurement1", first_measurement_value);
data.putDouble("measurement2", second_measurement_value);
data.putDouble("measurement3", third_measurement_value);

receiver.onMeasurementFinished(data);
}
}

Answer

You don't show your declaration of arg that shows up in newInstance and returnValues, but since you're using it in a static method it must be a static field.

That means there's only one per class, so when you say arg = measurement you're assigning different values to the same field over and over. That's why you only ever see one value.

Try this:

public static MeasurementFragment newInstance(String measurement){
    MeasurementFragment frag = new MeasurementFragment();
    Bundle args = new Bundle();

    // don't use the static arg here
    args.putString(ARG_MEASUREMENT, measurement);

    frag.setArguments(args);
    return frag;
}

private void returnValues(){
    if (checkMedian()){
        Bundle data = new Bundle();

        receiver = (MeasurementFinished) getActivity();
        // this is how you get a value set with setArguments
        String measurement = getArguments().getString(ARG_MEASUREMENT);
        data.putString("measurement", measurement);
        data.putDouble("mediana", median_value);
        data.putDouble("measurement1", first_measurement_value);
        data.putDouble("measurement2", second_measurement_value);
        data.putDouble("measurement3", third_measurement_value);

        receiver.onMeasurementFinished(data);
    }
}