0

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/

  1. 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.

viren jaru
  • 23
  • 2
  • 4

1 Answers1

0

For Outside App as well::
If you want this to run even if your app is closed, your best option is to use WorkManager library from JetPack.

I am not going to explain how to use WorkManager since documentation is already well written.

But your specific problem, you want to show timer in your activity so so you would store the id in local storage when the work starts.

In your activity, you will get the id and then get Work Status using WorkInfo object to get the time when job was started, calculate the remaining time and show it in activity.

This way, even if your user comes back to your activity, he/she will still get time remaining without problem.

Zohaib Amir
  • 3,482
  • 2
  • 11
  • 29
  • Just to confirm I want it only when application is running but activity is finished. Not app is closed. – viren jaru Jul 31 '19 at 09:58
  • @ZohainAmir. currently i have make an instance of timer class at application level so it will be active throughout the app. but i dont like that. so can you please suggest how to remove that member? and still save will be performed when timer get finished. Timer is only used in only one activity – viren jaru Jul 31 '19 at 10:05
  • As one of my main concern is viewmodel will be active even i left the activity. it is good not a good practice. i think and google document suggest – viren jaru Jul 31 '19 at 10:06