I am using handler in activity to run task on every second. when app goes in background.I started service as a foreground which takes duration from local storage. after long time (When app is in background) this task that i started in activity seems not running as duration is not updating on User Interface, I am using Active-Android As ORM for saving data to local storage. please suggest any work around how to overcome such situation i will post the code if needed any help is appreciated.
Asked
Active
Viewed 362 times
0
-
In which activity life cycle method (OnCreate/OnResume/OnPause) you called your handler? @Hardik Mehta – Nir Duan Aug 11 '16 at 08:03
-
@Nir Duan the architecture is such that there is seprate method which is called whenever user starts workout. i will post code if u want to see – Hardik Mehta Aug 11 '16 at 08:08
1 Answers
0
I would highly recommend change your app's design, Activity
not suppose to do any work that is not relevant to the UI attached to it.
In the current state the user create new thread from your activity and when the Android framework need additional resources (Can be hours after the activity is not visible anymore) it kills the activity and this is the reason why the UI doesn't seem to be updated.
My Solution-
Create a background service
to perform the "update every X second" operation and bound your activity to it when it's visible.
1. How to create bound service
2. What are activity life-cycle

Nir Duan
- 6,164
- 4
- 24
- 38
-
@ Nir Duan you are right but now many things tightly depand on activity so if i made some changes it could impact many things and i have to quickly fix such issue so is there any other workaround – Hardik Mehta Aug 11 '16 at 08:29
-
The only way to keep activity alive is to keep it visible @HardikMehta – Nir Duan Aug 11 '16 at 08:38
-
-
Sorry for the misspelled over the - There is no*** way... @HardikMehta – Nir Duan Aug 11 '16 at 08:47
-
@ Nir Duan so ultimately i have to change design ortherwize at some point activity may get killed – Hardik Mehta Aug 11 '16 at 08:48
-
Yes, and there is no way around it, even not hacky workarounds because it's deeply implemented in the android framework. @HardikMehta – Nir Duan Aug 11 '16 at 08:50
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120691/discussion-between-hardik-mehta-and-nir-duan). – Hardik Mehta Aug 11 '16 at 11:09