5

Say my app is running normally, showing Activity A. Then the user locks the phone. A dutifully goes to sleep (onPause()). At some point while the screen is off, I need to show something to the user. So from my background code (service), I call startActivity for B, passing the following flags:

FLAG_ACTIVITY_CLEAR_TOP
FLAG_ACTIVITY_NEW_TASK

And in onCreate() of B, I add the following window flags

FLAG_SHOW_WHEN_LOCKED 
FLAG_TURN_SCREEN_ON 
FLAG_DISMISS_KEYGUARD 

That all works fine - B now appears on top of the keyguard.

At some later point, I wish to dismiss B, so from within B I call dismiss(), which works out as expected. And here's where things go wrong: Instead of returning to the lock screen, I now have A running on top of the lock screen.

How can I stop the OS from resuming A when I dismiss B?

Thanks.

A--C
  • 36,351
  • 10
  • 106
  • 92
Stephan Steiner
  • 1,043
  • 1
  • 9
  • 22
  • I have a similar problem I haven't solved for months and am finally trying to figure it out. B shows fine, but once I dismiss it, once I unlock the lock screen, A pops up. I did get around it with some static variables, but it's not ideal. – A--C Jan 24 '13 at 21:53
  • After messing around a bit, I used `Intent.ACTIVITY_MULTIPLE_TASK` when launching my Activity since the Activity I show on the lock screen is independent - all it needs are some shared preferences, but doesn't rely on the other Activities. – A--C Jan 25 '13 at 00:20
  • Hmm.. in my case that won't work. In the list of recent apps I then have my app showing with screen B (or at times, no recent app).. and if my app was running prior to locking the phone (be it in the background or foreground), I'd like it to remain in the state it was prior to locking (so running.. either in background or foreground) – Stephan Steiner Jan 25 '13 at 10:06
  • i actually had an opposite problem - i wanted that activtiy A will still have the lockscreen dismissed . i think it's the default behavior. maybe you didn't mention something else that is related to the lock screen ? have you done anything to activity A ? – android developer Jan 26 '13 at 12:56
  • I'm not doing anything to A - A will be paused when the user locks the phone (or when the user presses the home key or back button when A is active (on screen)). – Stephan Steiner Jan 30 '13 at 11:03

1 Answers1

0

If I guess right than Android just shows the next Activity on the Activity stack belonging to your app. I would try to call another App e.g. the home screen on exit of Activity B, so that the lockscreen hopfully will get to the front.

You could also try to use the flag FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS:

If set, the new activity is not kept in the list of recently launched activities.

rekire
  • 47,260
  • 30
  • 167
  • 264
  • But calling that other activity without flags to show on lockscreen would indeed ensure that whatever activity I started would be visible once the screen was unlocked - however, it still deviates from the standard behavior of android which is to show the state of the phone prior to locking - that's what I'd like to replicate. I've come one step further.. re-locking the screen as per [the answer of this question](http://stackoverflow.com/questions/9633972/lock-phone-using-a-android-application?rq=1) will lead back to the lock screen - now only if A didn't show after unlock I'd be golden. – Stephan Steiner Jan 25 '13 at 09:49