0

If my activity is doing something in a background thread and now I want to display that activity in the foreground (because there's an alarm or something important to show), how can I do it?

I've seen this response but it creates a new activity.

I want to display the existing activity to the user, even when the user is using another app or even when the screen is off (here I know I'll need to use a WakeLock).

Thank you!

Community
  • 1
  • 1
Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100
  • Take a look at this: http://stackoverflow.com/questions/2424488/android-new-intent-starts-new-instance-with-androidlaunchmode-singletop – rafaelc Dec 10 '15 at 15:53
  • Thanks Rafael, but that doesn't seem to work. Maybe because I start the activity from the same activity? --- Yes, Mark, but that's the requirement. It's a special safety app that displays an important warning. – Ferran Maylinch Dec 10 '15 at 16:06

1 Answers1

2

I used the first solution from here but started the intent using the application context like it's done here, so the final code would be:

protected void bringActivityToForeground() {

    Intent intent = new Intent(this, getClass());
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    getApplicationContext().startActivity(intent);
}

I added that to my BaseActivity class.

In case you need it, you may also use a WakeLock to turn the screen on.

Community
  • 1
  • 1
Ferran Maylinch
  • 10,919
  • 16
  • 85
  • 100