public void calculateAndShow(boolean swapRanges) { float value = 24; UserBL userBL = new UserBL(getContext()); User user = userBL.getActiveUser(); BMI bmi = new BMI(user.getLatestHeight(), user.getLatestWeight()); value = Float.parseFloat(String.format(Locale.US, "%.2f", bmi.calculate())); String name = user.getName(); String age = String.valueOf(calculateAge(user.getBirthdate())); String height = String.valueOf(user.getLatestHeight()); textViewProfileInfoAge.setText(String.format(Locale.US, "%s سال", age)); textViewProfileInfoName.setText(String.format(Locale.US, "%s", name)); textViewProfileInfoHeight.setText(String.format(Locale.US, "%s سانتی متر", height)); gauge.setHighValue(value, 12, 44); textViewBMI.setText(String.valueOf(value)); textViewCurrentWeight.setText(String.format(Locale.US, "%s کیلوگرم", user.getLatestWeight())); FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); SharedPreferences sharedPreferences = getContext().getSharedPreferences("bmi", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); if (!swapRanges) { if (sharedPreferences.getInt("bmi_range", -1) == -1) { BmiRangeFragment bmiRangeFragment = new BmiRangeFragment(); fragmentTransaction.replace(R.id.linearLayoutRangeContainer, bmiRangeFragment); } else if (sharedPreferences.getInt("bmi_range", -1) == 1) { BmiRangeFragment bmiRangeFragment = new BmiRangeFragment(); fragmentTransaction.replace(R.id.linearLayoutRangeContainer, bmiRangeFragment); } else if (sharedPreferences.getInt("bmi_range", -1) == 2) { WeightRangeFragment weightRangeFragment = new WeightRangeFragment(); Bundle bundle = new Bundle(); bundle.putSerializable("bmi", bmi); weightRangeFragment.setArguments(bundle); fragmentTransaction.replace(R.id.linearLayoutRangeContainer, weightRangeFragment); } fragmentTransaction.commit(); } else { Fragment previousFragment = fragmentManager.findFragmentById(R.id.linearLayoutRangeContainer); if (previousFragment instanceof WeightRangeFragment) { BmiRangeFragment bmiRangeFragment = new BmiRangeFragment(); fragmentTransaction.setCustomAnimations(R.anim.scale_up, R.anim.scale_down) .replace(R.id.linearLayoutRangeContainer, bmiRangeFragment); editor.putInt("bmi_range", 1); } else if (previousFragment instanceof BmiRangeFragment) { WeightRangeFragment weightRangeFragment = new WeightRangeFragment(); Bundle bundle = new Bundle(); bundle.putSerializable("bmi", bmi); weightRangeFragment.setArguments(bundle); fragmentTransaction.setCustomAnimations(R.anim.scale_up, R.anim.scale_down) .replace(R.id.linearLayoutRangeContainer, weightRangeFragment); editor.putInt("bmi_range", 2); } fragmentTransaction.commit(); editor.apply(); } }
References
https://www.tutorialspoint.com/android/android_shared_preferences.htm