开发者

Application pause/resume state

My question is can i get to know when the entire application gets paused/resumed start/stop etc. For example if i have 5 activities in 开发者_运维技巧my application. Whenever any activity gets paused/resumed android notify the activity by calling the onPause/onResume methods.

So there are two possible scenarios when my activity gets paused. 1. My activity-2 gets paused because my activity-3 gets invoked. 2. My activity-2 gets paused because of some outside activity like incoming call.

Here I am interested only tracking when my activity gets paused by outside activities not my own application activities.

So is there any android provided solution for this or do I have to write my customized solution.

Thanks Dalvin


There is no solution provided by the API because it is not needed in most cases.

What you can do is to create an abstract activity and make all your activities inheriting from this abstract one. In this abstract activity, by overriding onCreate, onResume, onPause, onDestroy, you can manage to count how many of your own activities are "alive", and then determine the state of your application.

This could work but it's not really the Android phylosophy


You can know the starting of the whole application on application.oncreate() but there is no indicator for the whole application pause. Most of the cases never needs it anyway.

So further read in the activity lifecycle and the application class.

Still you can do this option in your program by overriding the onPause in each class and save a value to the sharedPrefrences then check on this value all over the application


If I understand your question, you want your app to be able to distinguish between exiting the current activity within the context of your program or by an external event like a phone call. I have used the following method in the past to do this (although it may not be the best, it definitely works):

(1) Set up a value in SharedPreferences (the built in file for storing a program's data). Call it something like "exitStatus" which is set to 1 for an exit within the program code and 0 for an exit based on external events.

(2) Now, within each of your activities, set the value of exitStatus to 0 in onResume (which is called no matter how you enter). If your program exits due to an external event within that activity, this value will persist when the program is reloaded.

(3) At the end of your activity, at all points where you are going to transfer to another activity, first set exitStatus to 1. Then, when you arrive at the other activity, it will know that you arrived there from within your program.

(4) Thus, just to be clear, each of your activities can check exitStatus at the outset to see whether you are entering from within your program context (= 1) or after a non-local exit of some kind (= 0).

That's all there is to it. I use this method to be sure that load data for my app is present as it may be lost if a user turns off their device so that the app tries to pick up in the middle of things when they later reboot, etc.


Instead of making base activity and override onPause/onResume you can use

registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback)

where you can handle these states for application activities in one place.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜