How to implement countdown timer [re-open activity]? - android

How i should implement a countdown timer if i need the following usage:
User starts activity A;
In activity A he starts the timer;
User leaves activity A, timer runs in background;
User comes back to activity A;
User see current (updated) values of the timer;
I tried to use a Countdown Timer but it fails at step 5 - after returning to ativity A i cant see values in TextView (in LogCat i do) and system starts another timer (this way, every activity re-open starts one more timer). Should I use service instead or ... show me the way, please.

You can't be sure that after you leave your activity it will be still alive. System could kill it at any time.
The solutions for this use case could be:
Start timer in foreground Service. Such types of services have better chances to be alive.
When you leave your activity you could save last timer value and last timestamp and restore timer with this info after activity is recreated.
PS. If you also want to fire the alarm when timer is finished you should use AlarmManager but keep in mind restrictions from android 6+ (M).

Related

how do i call a method from of a service from activity which i started the service and reopened

I have an activity and a service.
I want to start a countdown timer in the service and in the activity.
When I close the activity the timer in the service keeps on going in the background.
When I reopen the activity, I want to get the time remaining from the service's timer and start a new timer in the activity with the same time left. How can I accomplish this?
Bad implementation. You are running service just to run a timer to show the remaining time when you restart the app?
Instead you can do the following.
If you are starting timer now for 5 minutes, then when you start the timer, take current system time in milliseconds and add 5*60*1000, that is, you are recording the time your timer should end and then store this long variable in SharedPreference.
Next time when you start your app, get the value of finish time from SharedPreference and subtract it from your current time again. This will directly give you the remaining time in milliseconds.
No Service, no background thread, no additional running code, just a shared preference in the beginning of the timer will keep the record for you.

How to create a timer runed in the background and display one second of a second dynamically

How to create a timer runed in the background and display one second of a second in the TextView dynamically in MainActivity.Whatever I exit app or leaving the MainActivity,the timer also keep on.
I thinked that I can use service and BroadcastReceiver or using Handler and Thread.But I can't solve it.
Having an always-on service to simply count up seconds is a bad idea, it'll waste battery life and could be killed at any time.
What you could do is something like this:
Make a counter that you can start/stop with a button.
On your your activity's onPause, save the System.currentTimeInMillis() along with the current count on your timer
When you resume your activity, use the current System.currentTimeInMillis() value to calculate what your timer should be displaying had it been really running all that time.
I have thinked a solution; Thread+Handler; EveryTime enter ther MainActivity
we just set the time interval = EndTime-System.currentTimeMillis();

Pausing Android timer running in activity

I have a timer running in my activity. Let's say user switches to another activity. Should i pause my timer in that case' because sometimes my whole activity gets killed when i resume my application?
Hold your timer remaining time/value/etc into a global static object. When you switch back, use it for your timer or design a good application lifecycle with onPause, onResume etc.

How to keep the activity state intact on back press in android?

I am new to Android . As per the Android Developers Doc making an activity launchmode singleTop it will keep that activity intact . But its not working for me .I have an Activity where i have a countdown timer , what i want is when i leave that Activity on back press and return to that Activity that countdown timer should still be running . How to do it ? Please Help
I believe you misunderstood a bit.
Launching an activity in singleTop does not mean that the activity is "intact", it means that if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent - a new instance won't be created. (This is opposite to launching activities in standard mode, which every time there's a new intent a new instance of the class is created to respond to that intent.)
As others suggested, you could bind to a Service and update the countdown time from there.
"Creating a Service will always keep that timer running , instead i want it to keep running only when application is alive", that means you want to continue the timer from where the user left.
Store the timer value in SharedPreferences onStop() and retrieve the same onRestart() and then finally continue updating

android: How can I send an activity to background & call her from there

I am in process of creating some test application "Time Tracker" app like stopwatch so I start app go to Activity where I start time counter and now I need some functionality to send my activity to background or minimize or hide and after some period to call this activity again and stop her counter to get passed time period .
How can I send Activity to back and call her to front but not to stop timer counter ??
Using a Service would be the best way I guess. You can than call finish() on your Activity and the Service can start your application when a specific time was reached.

Categories

Resources