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.
source share