I want to create a one activity which is having a timer. lets say for 10 minute. i want to show time in activity. on time finish i want to save data into the server.
there are two use case for saving data. 1. user is on timer screen this case is handled when timer finish, as activity is running it will get trigger into viewmodel and call repository api to save data/
- user left the timer screen here is the problem i want to save the data even user finished the activity. from where should i call the save method to network.
please suggest solution for above number 2 and if there is any better solution than current is also welcome.
currently i have timer class in ViewModel and when user left activity i have assigning that class into global application level variable of same class so it will be active even i finished the activity and it will call the save method to network.
class Activity{
//observer from viewmodel to show time update using live data
//time observed here to display in text view
}
enter code here
class ViewModel :ViewModel{
long time:Livedata<> // this is observed into the activity
save(){
repository.save() //save method of network call from repository
}
// this class instance is also saved when i finish activity into //application level member. provide solution for this..
inner class timer :CountDownTimer(millisInFuture){
// updated time member of viewmodel
}
}
class repository{
save(){
//network call to save data.
}
}
please suggest solution for where should be timer class and how to save data via repository when activity is finished and if there is any better solution than current is also welcome.
i know viewmodel and repository will be active even when i finished the activity and it will leak memory but i am not getting better solution.