The Android application goes to the background and returns to the foreground to determine the method

We know that when we press the back or home key to exit the application interface, the application will be suspended in the background. The advantage of this design is that since the application is cached in memory by the system, the user can quickly start the application by calling the cache when opening and starting the application. Of course, due to today's memory and processor capacity constraints, the system will automatically shut down some processes.

So how do I judge whether the application is in the foreground or in the background, and the application returns to the foreground from the background?

It is generally runningprocess or runningtasks to judge whether the application is in the foreground or background

In fact, Android provides a callback in SDK 14. Activitylifecyclecallbacks, you can get the life cycle callbacks of all app activities through this callback.

This callback is written in the application. You can register when the application is initialized.

Through these callbacks, we can put this count in OnStart () and onstop (). If activitycount = = 1, it is the foreground. If activitycount = = 0, it is the background. That's a simple judgment.

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>