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?
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With