Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

count down timer problem

I have 2 activities in my app ActivityA and ActivityB and a count down timer. The timer should keep running between the two activities. If the user clicks on the home or back button the timer will be saved to database and will then continue from the last state on app relaunch.

I am saving to the database in onPuse() and in onBackPressed(). To save data if user clicks on home and back button.

My problem is: If ActivityB is called from ActivityA, then save to database method will be called. How can I avoid it being called unless home button is pressed?

like image 663
Kishore Avatar asked May 11 '26 17:05

Kishore


1 Answers

As soon as the timer is created, store the creation time in database and then in each activities onResume just reload the timer starting from current time - created time. Something like this psuedo code

createTimer() {
    createTimerEntryInDB();
    showTimer();
}

showTimer() {
   fetchTimerEntryFromDB();
   populateTimerUI();
}

/* In both activity */
onResume() {
   if (doesTimerExistInDB) {
       showTimer(); 
   }
}
like image 107
PravinCG Avatar answered May 13 '26 05:05

PravinCG



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!