android – Why the variables numer, inaltime, greutate from SharedViewModelClass doesn’t retain the values ​​I pass throught the InputText?

SharedViewModelClass:


class SharedViewModel : ViewModel() {
    private var _nume= MutableLiveData<String>("")
    val nume : LiveData<String> = _nume


    private var _gen= MutableLiveData<String>("")
    val gen : LiveData<String> = _gen


    private var _inaltime= MutableLiveData<Double>(0.0)
    val inaltime : LiveData<Double> = _inaltime


    private var _greutate= MutableLiveData<Double>(0.0)
    val greutate : LiveData<Double> = _greutate

    private var _activitate= MutableLiveData<String>("")
    val activitate : LiveData<String> = _activitate

    private var _ideal= MutableLiveData<Double>(0.0)
    val ideal : LiveData<Double> = _ideal


    fun setNumele(Numele: String){
        _nume.value= "$Numele "
    }

    fun setGen(Gen: String){
        _gen.value= Gen
    }

    fun setInaltime(Inaltime: Double){
        _inaltime.value= Inaltime
    }

    fun setGreutate(Greutate: Double){
        _greutate.value= Greutate
    }

    fun setActivitate(Activitate: String){
        _activitate.value= Activitate
        calculIdeal()
    }



    private fun calculIdeal(){


        if(_gen.value=="Barbat"){
            _ideal.value = 2200.0
        }
        if(_gen.value=="Femeie"){
            _ideal.value = 1800.0
        }

        if( (_inaltime.value ?: 0.0) - 100 == (_greutate.value ?: 0.0))
        {
            _ideal.value= (_ideal.value ?: 0.0)
        }
        if(( _greutate.value ?: 0.0)  +10 <= (_inaltime.value ?: 0.0) -100)
        {
            _ideal.value= (_ideal.value ?: 0.0) +300
        }
        if(( _greutate.value ?: 0.0)  -10 >= (_inaltime.value ?: 0.0) -100)
        {
            _ideal.value= (_ideal.value ?: 0.0) -300
        }

        when(_activitate.value)
        {
            "Deloc" -> {
                _ideal.value =(_ideal.value ?: 0.0)
            }
            "O data pe saptamana" -> {
                _ideal.value = (_ideal.value ?: 0.0) + 200
            }
            "De trei sau mai multe ori pe saptamana" -> {
                _ideal.value = (_ideal.value ?: 0.0) + 500
            }
        }

    }


}

This is all the code from SharedViewModelClass… so the inaltime, greutate,nume are the variables from TextInput(these i think doesn’t retain any value and idk why), the rest are from RadioButton( these work)

In the first fragment:

class StartFragment : Fragment() {

    private var binding: FragmentStartBinding?   =null
    private  val sharedViewModel: SharedViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragmentBinding = FragmentStartBinding.inflate(inflater, container, false)
        binding= fragmentBinding
        return fragmentBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding?.apply {
            viewModel= sharedViewModel
            startFragment=this@StartFragment
            lifecycleOwner= viewLifecycleOwner
        }
    }

 fun goToNextScreen() {

     val nume = binding?.numeEditText?.text.toString()
     sharedViewModel.setNumele(nume)


     val inaltime = binding?.inaltimeEditText?.text.toString().toDouble()
     sharedViewModel.setInaltime(inaltime)

     val greutate = binding?.greutateEditText?.text.toString().toDouble()
     sharedViewModel.setGreutate(greutate)



     findNavController().navigate(R.id.action_startFragment_to_resultFragment)
 }



}

In the second fragment:

class ResultFragment : Fragment() {

    private var binding: FragmentResultBinding?   =null
    private  val sharedViewModel: SharedViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragmentBinding = FragmentResultBinding.inflate(inflater, container, false)
        binding= fragmentBinding
        return fragmentBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding?.apply {
            viewModel= sharedViewModel
            resultFragment=this@ResultFragment
            lifecycleOwner= viewLifecycleOwner
        }
    }


}

In the layout of the first fragment I pass the button:

android:onClick="@{() -> startFragment.goToNextScreen()}" 

But idk why it doesn’t retain the value of TextInputs

In the layout of the first fragment I also have an RadioButton whom I pass:

android:checked="@{viewModel.activitate.equals(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"

android:onClick="@{() -> viewModel.setActivitate(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"

And this 2 lines seems to retain the values ​​for the RadioButtons

Should I pass anything in the TextInputLayout or is something that I miss ????

****XML for the first fragment(start):****

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".StartFragment">
    
        <data>
            <variable
                name="startFragment"
                type="com.example.dieta.StartFragment" />
            <variable
                name="viewModel"
                type="com.example.dieta.model.SharedViewModel" />
        </data>
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="16dp">
    
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/nume"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="10dp"
                android:hint="Introduceti numele"
                app:layout_constraintTop_toTopOf="parent"
                app:boxBackgroundColor="@color/white">
    
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/nume_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPersonName"
                    android:textColorHint="#3f51b5">
    
                </com.google.android.material.textfield.TextInputEditText>
            </com.google.android.material.textfield.TextInputLayout>
    
            <TextView
                android:id="@+id/gen_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:layout_marginTop="30dp"
                android:padding="8dp"
                android:text="Alegeti genul :"
                android:textSize="18dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/nume">
    
            </TextView>
    
            <RadioGroup
    
                android:id="@+id/gen_optiune"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
    
                android:orientation="horizontal"
                app:layout_constraintStart_toStartOf="@id/gen_txt"
                app:layout_constraintTop_toBottomOf="@id/gen_txt">
    
                <RadioButton
                    android:id="@+id/barbat"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="20dp"
                    android:buttonTint="#3f51b5"
                    android:checked="@{viewModel.gen.equals(@string/barbat)}"
                    android:onClick="@{() -> viewModel.setGen(@string/barbat)}"
                    android:text="@string/barbat">
    
                </RadioButton>
    
                <RadioButton
                    android:id="@+id/femeie"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:buttonTint="#3f51b5"
                    android:checked="@{viewModel.gen.equals(@string/femeie)}"
                    android:onClick="@{() -> viewModel.setGen(@string/femeie)}"
                    android:text="@string/femeie">
    
                </RadioButton>
    
            </RadioGroup>
    
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/inaltime"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp"
                android:hint="Introduceti inaltimea"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias=".32"
                app:boxBackgroundColor="@color/white">
    
    
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/inaltime_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:textColorHint="#3f51b5"
                    >
    
                </com.google.android.material.textfield.TextInputEditText>
            </com.google.android.material.textfield.TextInputLayout>
    
    
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/greutate"
                android:layout_width="200dp"
                android:layout_height="50dp"
                android:layout_marginStart="10dp"
                android:hint="Introduceti greutatea"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintVertical_bias=".45"
                app:boxBackgroundColor="@color/white">
    
    
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/greutate_edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="number"
                    android:textColorHint="#3f51b5">
    
                </com.google.android.material.textfield.TextInputEditText>
            </com.google.android.material.textfield.TextInputLayout>
    
    
            <TextView
                android:id="@+id/activitate_txt"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:layout_marginTop="50dp"
                android:padding="8dp"
                android:text="De cate ori faceti sport pe saptamana ?"
                android:textSize="18dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/greutate">
    
            </TextView>
    
            <RadioGroup
                android:id="@+id/activitate_optiuni"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="4dp"
                android:layout_marginTop="10dp"
    
                android:orientation="vertical"
                app:layout_constraintStart_toStartOf="@id/activitate_txt"
                app:layout_constraintTop_toBottomOf="@id/activitate_txt">
    
                <RadioButton
                    android:id="@+id/optiune_deloc"
                    android:layout_width="155dp"
                    android:layout_height="wrap_content"
                    android:buttonTint="#3f51b5"
                    android:checked="@{viewModel.activitate.equals(@string/deloc)}"
                    android:onClick="@{() -> viewModel.setActivitate(@string/deloc)}"
                    android:text="@string/deloc" />
    
                <RadioButton
                    android:id="@+id/optiune_o_data"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:buttonTint="#3f51b5"
                    android:checked="@{viewModel.activitate.equals(@string/o_data_pe_saptamana)}"
                    android:onClick="@{() -> viewModel.setActivitate(@string/o_data_pe_saptamana)}"
                    android:text="@string/o_data_pe_saptamana" />
    
                <RadioButton
                    android:id="@+id/optiune_de_trei_sau_mai_multe_ori"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:buttonTint="#3f51b5"
                    android:checked="@{viewModel.activitate.equals(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"
                    android:onClick="@{() -> viewModel.setActivitate(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"
                    android:text="@string/de_trei_sau_mai_multe_ori_pe_saptamana" />
    
            </RadioGroup>
    
            <Button
                android:id="@+id/buton1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:backgroundTint="#3f51b5"
                android:onClick="@{() -> startFragment.goToNextScreen()}"
                android:text="Calculati"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.25"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/activitate_optiuni">
    
            </Button>
    
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    
    </layout>


<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".StartFragment">

    <data>
        <variable
            name="startFragment"
            type="com.example.dieta.StartFragment" />
        <variable
            name="viewModel"
            type="com.example.dieta.model.SharedViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/nume"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="10dp"
            android:hint="Introduceti numele"
            app:layout_constraintTop_toTopOf="parent"
            app:boxBackgroundColor="@color/white">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/nume_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textPersonName"
                android:textColorHint="#3f51b5">

            </com.google.android.material.textfield.TextInputEditText>
        </com.google.android.material.textfield.TextInputLayout>

        <TextView
            android:id="@+id/gen_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="20dp"
            android:layout_marginTop="30dp"
            android:padding="8dp"
            android:text="Alegeti genul :"
            android:textSize="18dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/nume">

        </TextView>

        <RadioGroup

            android:id="@+id/gen_optiune"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"

            android:orientation="horizontal"
            app:layout_constraintStart_toStartOf="@id/gen_txt"
            app:layout_constraintTop_toBottomOf="@id/gen_txt">

            <RadioButton
                android:id="@+id/barbat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginEnd="20dp"
                android:buttonTint="#3f51b5"
                android:checked="@{viewModel.gen.equals(@string/barbat)}"
                android:onClick="@{() -> viewModel.setGen(@string/barbat)}"
                android:text="@string/barbat">

            </RadioButton>

            <RadioButton
                android:id="@+id/femeie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="#3f51b5"
                android:checked="@{viewModel.gen.equals(@string/femeie)}"
                android:onClick="@{() -> viewModel.setGen(@string/femeie)}"
                android:text="@string/femeie">

            </RadioButton>

        </RadioGroup>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/inaltime"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginStart="10dp"
            android:hint="Introduceti inaltimea"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias=".32"
            app:boxBackgroundColor="@color/white">


            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/inaltime_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:textColorHint="#3f51b5"
                >

            </com.google.android.material.textfield.TextInputEditText>
        </com.google.android.material.textfield.TextInputLayout>


        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/greutate"
            android:layout_width="200dp"
            android:layout_height="50dp"
            android:layout_marginStart="10dp"
            android:hint="Introduceti greutatea"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias=".45"
            app:boxBackgroundColor="@color/white">


            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/greutate_edit_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="number"
                android:textColorHint="#3f51b5">

            </com.google.android.material.textfield.TextInputEditText>
        </com.google.android.material.textfield.TextInputLayout>


        <TextView
            android:id="@+id/activitate_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginTop="50dp"
            android:padding="8dp"
            android:text="De cate ori faceti sport pe saptamana ?"
            android:textSize="18dp"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/greutate">

        </TextView>

        <RadioGroup
            android:id="@+id/activitate_optiuni"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="4dp"
            android:layout_marginTop="10dp"

            android:orientation="vertical"
            app:layout_constraintStart_toStartOf="@id/activitate_txt"
            app:layout_constraintTop_toBottomOf="@id/activitate_txt">

            <RadioButton
                android:id="@+id/optiune_deloc"
                android:layout_width="155dp"
                android:layout_height="wrap_content"
                android:buttonTint="#3f51b5"
                android:checked="@{viewModel.activitate.equals(@string/deloc)}"
                android:onClick="@{() -> viewModel.setActivitate(@string/deloc)}"
                android:text="@string/deloc" />

            <RadioButton
                android:id="@+id/optiune_o_data"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="#3f51b5"
                android:checked="@{viewModel.activitate.equals(@string/o_data_pe_saptamana)}"
                android:onClick="@{() -> viewModel.setActivitate(@string/o_data_pe_saptamana)}"
                android:text="@string/o_data_pe_saptamana" />

            <RadioButton
                android:id="@+id/optiune_de_trei_sau_mai_multe_ori"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="#3f51b5"
                android:checked="@{viewModel.activitate.equals(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"
                android:onClick="@{() -> viewModel.setActivitate(@string/de_trei_sau_mai_multe_ori_pe_saptamana)}"
                android:text="@string/de_trei_sau_mai_multe_ori_pe_saptamana" />

        </RadioGroup>

        <Button
            android:id="@+id/buton1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:backgroundTint="#3f51b5"
            android:onClick="@{() -> startFragment.goToNextScreen()}"
            android:text="Calculati"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.25"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/activitate_optiuni">

    </Button>


</androidx.constraintlayout.widget.ConstraintLayout>
**XML for the second freagment(result):**

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">


    <data>
        <variable
            name="resultFragment"
            type="com.example.dieta.ResultFragment" />
        <variable
            name="viewModel"
            type="com.example.dieta.model.SharedViewModel" />
    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ResultFragment"
        android:background="@drawable/aplauze">


        <TextView
            android:layout_width="388dp"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="150dp"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="80dp"
            android:layout_marginBottom="629dp"
            android:text="Felicitari, ai completat datele cu succes!"
            android:textSize="21dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="192dp"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginTop="150dp"
            android:layout_marginEnd="63dp"
            android:layout_marginRight="80dp"
            android:layout_marginBottom="316dp"
            android:text="@{viewModel.ideal.toString()}"
            android:textSize="30dp"
            android:textStyle="bold" />

        <ImageView
            android:layout_width="181dp"
            android:layout_height="178dp"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginEnd="106dp"
            android:layout_marginBottom="440dp"
            android:src="@drawable/ic_tick">

        </ImageView>

        <TextView
            android:layout_width="388dp"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="45dp"
            android:layout_marginEnd="-23dp"
            android:layout_marginRight="80dp"
            android:layout_marginBottom="401dp"
            android:text="Idealul tau zilnic de calorii este:"
            android:textSize="21dp"
            android:textStyle="bold" />

    </RelativeLayout>
</layout>

Leave a Comment