I am trying to write an app that does something specific when it is brought back to the foreground after some amount of time. Is there a way to detect when an app is sent to the background or brought to the foreground?
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
Edit: the new architecture components brought something promising: ProcessLifecycleOwner, see @vokilam's answer
The actual solution according to a Google I/O talk:
Yes. I know it's hard to believe this simple solution works since we have so many weird solutions here.
But there is hope.
My app needs to "reboot" after return from background - show a series of activities, according to client solicitations. After extensive search on how to manage the background/foreground transitions (treated very differently between iOS and Android), I crossed this question. Found very useful help here, specially from the most voted answer and the one flagged as correct. However, simply reinstantiate the root activity EVERY TIME the app enters foreground looked too annoying, when you think about UX. The solution that worked for me, and the one I think's most adequated - based on the Youtube and Twitter apps functionality - was to combine the answers from @GirishNair and @d60402: Calling the timer when the app's trimming memory, as follows:
My Timer limit is set to 30 seconds - I'm thinking about increasing this a little.
And when app goes into foreground, is relaunched, or the app's destroyed, call the method to cancel timer.
On App extension:
On the activity (preferably on a base activity, inherited by the others):
In my case, when app goes foreground after the max time, a new task is created, so the stopActivityTransitionTimer() is called upon onActivityCreated() or onActivityDestroyed(), in the app extension class - turning unnecessary to call the method in an activity. Hope it helps.
If your app consists of multiple activites and/or stacked activites like a tab bar widget, then overriding onPause() and onResume() will not work. I.e when starting a new activity the current activites will get paused before the new one is created. The same applies when finishing (using "back" button) an activity.
I've found two methods that seem to work as wanted.
The first one requires the GET_TASKS permission and consists of a simple method that checks if the top running activity on the device belongs to application, by comparing package names:
This method was found in the Droid-Fu (now called Ignition) framework.
The second method that I've implemented my self does not require the GET_TASKS permission, which is good. Instead it is a little more complicated to implement.
In you MainApplication class you have a variable that tracks number of running activities in your application. In onResume() for each activity you increase the variable and in onPause() you decrease it.
When the number of running activities reaches 0, the application is put into background IF the following conditions are true:
When you can detect that the application has resigned to the background it is easy detect when it is brought back to foreground as well.
Edit 2: What I've written below will not actually work. Google has rejected an app that includes a call to ActivityManager.getRunningTasks(). From the documentation, it is apparent that this API is for debugging and development purposes only. I'll be updating this post as soon as I have time to update the GitHub project below with a new scheme that uses timers and is almost as good.
Edit 1: I've written up a blog post and created a simple GitHub repository to make this really easy.
The accepted and top rated answer are both not really the best approach. The top rated answer's implementation of isApplicationBroughtToBackground() does not handle the situation where the Application's main Activity is yielding to an Activity that is defined in the same Application, but it has a different Java package. I came up with a way to do this that will work in that case.
Call this in onPause(), and it will tell you if your application is going into the background because another application has started, or the user has pressed the home button.
Correct Answer here
Create class with name MyApp like below:
Then, everywhere you want (better first activity launched in app), add the code below:
Done! Now when the app is in the background, we get log
status : we are out
and when we go in app, we get logstatus : we are out
The
onPause()
andonResume()
methods are called when the application is brought to the background and into the foreground again. However, they are also called when the application is started for the first time and before it is killed. You can read more in Activity.There isn't any direct approach to get the application status while in the background or foreground, but even I have faced this issue and found the solution with
onWindowFocusChanged
andonStop
.For more details check here Android: Solution to detect when an Android app goes to the background and come back to the foreground without getRunningTasks or getRunningAppProcesses.