How to Bring Android Activity to the Foreground and Detect if its in background?
开发者_StackOverflow中文版I have a callback inside an Activity which is called by a remote service. I fire a Toast when it gets called so, i know its getting called but its in the background, so I need a way from within the activity to make it go to the foreground. How can this be done. Also detect if I am in Foreground or not. How can this be done?
Using the Android Lifecycle you can detect if your Activity is in the background. To return your Activity to the foreground use a Notification in the Status Bar. You can forcibly move your Activity to the foreground by firing an Intent but that can cause frustration and annoyance to the user.
Just call startActivity with the right Intent.
Edit: Forgot to say, that it'a a very confusing behaviour, if an Activity brings itself to the front. The user could do something else and be disturbed. Set a notification instead.
精彩评论