Application Pause / Resume Status

My question is: can I find out when the whole application pauses / resumes start / stop, etc. For example, if I have 5 activities in my application. Whenever any action is paused / resumed, the android notifies you of the activity by calling the onPause / onResume methods.

So there are two possible scenarios when my activity pauses. 1. My activity-2 is suspended, as my activity-3 is activated. 2. My activity-2 is suspended due to some external activity, such as an incoming call.

Here I'm only interested in tracking, when my activity is suspended by external actions, and not by my own application actions.

So, is there any solution for Android, or should I write my customized solution.

Thanks Dalvin

+6
source share
4 answers

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

What you can do is create an abstract activity and make all your actions inherited from this abstract. In this abstract action, overriding onCreate, onResume, onPause, onDestroy, you can calculate how many of your own actions are โ€œlivingโ€, and then determine the state of your application.

It may work, but itโ€™s not really Android philosophy

+10
source

You can find out the beginning of the entire application on application.oncreate() , but there is no indicator for the entire pause of the application. Most cases never need this.

So read the life cycle and application in action.

However, you can make this option in your program by overriding onPause in each class and store the value in sharedPrefrences , then check this value throughout the application

+3
source

If I understand your question, you want your application to be able to distinguish between an exit from an ongoing activity in the context of your program or an external event, for example, by phone. I used the following method in the past to do this (although this may not be the best, it definitely works):

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

(2) Now, as part of each of your actions, set exitStatus to 0 in onResume (which is called no matter how you enter). If your program exits due to an external event in this action, this value will be saved when the program is reloaded.

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

(4) Thus, to be clear, each of your actions can first check exitStatus to see if you enter from the program context (= 1) or after a non-local exit of any kind (= 0).

That's all. I use this method to make sure that the data for my application is loading, because it can be lost if the user disconnects his device, so that the application tries to occupy the middle of things when they later reboot, etc.

+3
source

Instead of doing basic activity and overriding onPause / onResume, you can use

 registerActivityLifecycleCallbacks(Application.ActivityLifecycleCallbacks callback) 

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

0
source

Source: https://habr.com/ru/post/889682/


All Articles