0
0
007hrist2020-11-09 01:55:39
Java
007hrist, 2020-11-09 01:55:39

How to start a timer in the background?

The task is this. There is an application in which fragment1 and fragment2 are present. Fragment1 has a timer that randomizes messages and displays them in the RecyclerView and in the BottomNavigationView - the counter of these same messages. Everything works fine until fragment2 opens. The timer stops working and updates the counter. Displaying the timer in the activity is not an option, because. RecyclerView cannot be updated from activity. How to make the timer work with any open fragment, where to put it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petya, 2020-11-10
@007hrist

keep the timer inside the ViewModel and network the value of the timer in LiveData, and from the fragment just subscribe to LiveData

O
Oleg Zakharov, 2020-11-09
@blazenn12

Move timer to service

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question