My Android app has a TextView telling the user the data age (“13 minute ago”). I currently update this every second using a Runnable to catch when the minute changes with a notifyDataSetChanged() to my adapter. But this is causing garbage collection every second…
Is there another method to trigger a TextView update (or take general action) when the system clock changes minute without checking every second for such a change?
Since you already set up a Runnable and callback, use modulo to calculate when the next update should happen:
This will run the callback when the next minute occurs whether it is 59, 30, or 1 second(s) away. As a note, if the Runnable only updates one TextView don;t forget to switch to
DateUtils.HOUR_IN_MILLISwhen the event is an hour old, no sense updating “1 hour ago” every minute.You may find methods like
DateUtils.getRelativeTimeSpanString()useful for creating the “x minutes ago” strings.