android – Kotlin: Environmental chronometer timer handled from 2 fragments

I have two chronometers in two fragments and I need them to have the same associated time. However, right now the behavior is that the first chronometer works correctly until I change the fragment and its time is reset. The second timer doesn’t work and I would like the first and second times to receive the same time and not be reset every time I change fragments. I would also like to be able to access the chronometer time from any other fragment of the application and if I wish I could start or stop the general chronometer. I know how I can start and stop a simple timer (timer.start() or timer.stop()). How could I solve it? My code is the following: First Fragment code (xml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000000">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:orientation="vertical">
        <!--Change for roboto font-family-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sport_2"
            android:fontFamily="@font/roboto_bold"
            android:textSize="13sp"
            android:textColor="#ffffff"
            android:gravity="center"/>

            <Chronometer
                android:id="@+id/ct"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:textColor="#ffffff" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawable_cal"/>
            <TextView
                android:id="@+id/tvCal"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/cal"
                android:fontFamily="@font/roboto_bold"
                android:textSize="13sp"
                android:layout_marginStart="10dp"
                android:textColor="#ffffff"
                android:gravity="center"/>

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="15dp"
                android:src="@drawable/drawable_bpm"/>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_gravity="center"
                android:gravity="center"
                android:layout_marginStart="10dp">
                <TextView
                    android:id="@+id/tvBpm"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/bpm"
                    android:textSize="13sp"
                    android:fontFamily="@font/roboto_bold"
                    android:textColor="#ffffff"
                    android:gravity="center"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/bpmText"
                    android:fontFamily="@font/roboto_regular"
                    android:textSize="10sp"
                    android:textColor="#ffffff"
                    android:gravity="center"/>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:orientation="horizontal">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/drawable_o2"/>
            <TextView
                android:id="@+id/tvO2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/o2"
                android:fontFamily="@font/roboto_bold"
                android:textSize="13sp"
                android:layout_marginStart="10dp"
                android:textColor="#ffffff"
                android:gravity="center"/>

            <Button
                android:layout_width="59dp"
                android:layout_height="20dp"
                android:background="@drawable/button_o2"
                android:layout_marginStart="10dp"/>
            </LinearLayout>
        <ImageButton
            android:id="@+id/btCronometerOperation"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:background="@drawable/button_pause"
            android:layout_marginTop="20dp"
            />
        </LinearLayout>


</androidx.wear.widget.BoxInsetLayout>

Code for first fragment:

class StandardExerciseFragment : Fragment(){

    private lateinit var binding: StandardExerciseFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        startCronometer()
        binding = StandardExerciseFragmentBinding.inflate(inflater, container, false)
        action_cronometer_button()
        return binding.root
    }
    fun action_cronometer_button(){
        binding.btCronometerOperation.setOnClickListener {
            (activity as MainActivity).openFragment(CronometerManagerFragment())
        }
    }
    fun startCronometer(){
        binding.ct.start()
    }
}

Second fragment code (class):

class CronometerManagerFragment : Fragment(){

    private lateinit var binding: CronometerManagerFragmentBinding

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): View {
        binding = CronometerManagerFragmentBinding.inflate(inflater, container, false)
        resume()
        return binding.root
    }
    fun resume(){
        binding.btResume.setOnClickListener {
            (activity as MainActivity).openFragment(StandardExerciseFragment())
        }
    }
}

Second fragment (xml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#000000">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:orientation="vertical">
        <!--Change for roboto font-family-->
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/sport_2"
                android:fontFamily="@font/roboto_bold"
                android:textSize="13sp"
                android:textColor="#ffffff"
                android:gravity="center"/>

            <Chronometer
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="20dp"
                android:textColor="#ffffff" />
        </LinearLayout>
        <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/button_pause"
            android:layout_marginTop="20dp"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <Button
                android:id="@+id/btResume"
                android:layout_width="50dp"
                android:layout_height="20dp"
                android:background="@drawable/button_resume"
                android:layout_marginTop="20dp"
                />
            <Button
                android:id="@+id/btStop"
                android:layout_width="50dp"
                android:layout_height="20dp"
                android:layout_marginStart="10dp"
                android:background="@drawable/button_stop"
                android:layout_marginTop="20dp"
                />
        </LinearLayout>
    </LinearLayout>
</androidx.wear.widget.BoxInsetLayout>

Activity fragment (xml):

<?xml version="1.0" encoding="utf-8"?>
<androidx.wear.widget.BoxInsetLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    tools:deviceIds="wear">

    <FrameLayout
        android:id="@+id/mainActivityFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.wear.widget.BoxInsetLayout>

Leave a Comment