1

I have an Android app with an AlarmManager that repeats every 15 minutes. Depending on the results from this I want to update the UI, but I don't want Android to bring the app to the foreground if it doesn't have focus already.

How can I do this? Alternatively, how can I make the UI updates run in onResume?

I'm calling methods like public static void updateRunningStatusTextView(Boolean inStatus) on the main activity from services and classes

CeeRo
  • 1,142
  • 3
  • 12
  • 21

3 Answers3

0
@Override
protected void onRestart() {
    super.onRestart();
    new Handler().postDelayed(new Runnable() {

        public void run() {
            // update UI here from cached values
        }
    }, 100);
}
  • Isn't onResume a better place to handle this, since it gets called after onRestart anyway and catches a couple other cases too? And do I really need to use a Handler and Runnable? – CeeRo May 30 '13 at 15:59
0

I'm pretty sure your first option can't happen. There is postInvalidate() that can update the View from a non UI thread but this is available

...only when this View is attached to a window.

according to the Docs

Alternatively, how can I make the UI updates run in onResume?

This depends on many things such as the View, the data, and what you have in your code but you could set the data in a static class or save it in something like SharedPreferences then retrieve the data from there, say a String to use in setText() of a TextView

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • I am already saving the relevant variables in sharedpreferences, so I guess putting an updateUI() method in onResume is the way to go. If the app has focus I want to update immediately though, how would I implement that? – CeeRo May 30 '13 at 15:56
  • I guess it kind of depends on what you have going on but one option is to call `invalidate()` on the `Views` that need to be refreshed – codeMagic May 30 '13 at 16:08
  • I mean the "check if app is in foreground"-process..? – CeeRo May 30 '13 at 16:12
  • 1
    See [this answer](http://stackoverflow.com/questions/8489993/check-android-application-is-in-foreground-or-not) it should give you what you need – codeMagic May 30 '13 at 16:14
  • I went for the Boolean isVisible-method, seems to work fine so far. Thanks for the help – CeeRo May 31 '13 at 12:25
0

Well, you don't. If it is in background, you don't need to update the UI!

Then you just need to update the UI when you resume your app (onResume), with the latest data you have.

thiagolr
  • 6,909
  • 6
  • 44
  • 64