I'd like to do certain, simple action every time that my app is closing - I just want to decrease int variable. How to do that?
2 Answers
As @Ozair has rightly asked that it depends what do you define as closing of app
.
You can do either or both of the following depending on the need:
If you want to detect closing of app by use of BACK button, then from within your last activity, you can detect the pressing of BACK button by overriding onBackPressed function. There you can decrement your value.
If you are also considering the situation when you app goes into the background by pressing of HOME button, then in your activities you would have to detect the HOME button pressed. There have been many solutions which no more work for detecting HOME button but this answer on How can I detect user pressing HOME key in my activity? question seems to work for me. So, there you can detect the HOME button and decrement the value which you can save in
SharedPreferece
.
There can be other cases where you are calling finish()
and closing your last activity. It is not clear from your question if you are considering that case as well.
Hope this gives you some opportunity to think about it.

- 1
- 1

- 25,769
- 11
- 95
- 124
-
I'd define closing application as killing it's process. My goal is to download file everytime the app runs - in my case it has no sense in downloading kinda large file when app isn't active. – ZZ 5 Aug 17 '13 at 22:48
-
1Again by killing do you mean `Force Stop` or being killed by the OS? Or do you just mean that the App is being closed by the user. – Shobhit Puri Aug 17 '13 at 23:04
-
Well, I'd be interested in every case, but mainly the situation when the App is being closed by the user – ZZ 5 Aug 19 '13 at 12:30
-
1If by the user then the above two coases would suffice as those are the two buttons which would enable the user to close it. Other than that if you have any buttons within the app, then you would need to include that case as well. – Shobhit Puri Aug 19 '13 at 14:45
-
and what about situations when the app is killed by OS or force stopped? Or maybe there's better solution to force app to do certain action everytime it's ran? – ZZ 5 Aug 20 '13 at 06:01
-
1If you want to count that too, then there are ways. First if user force stops the app by going to `Settings`, then either the app would already be closed or in the background. How else would he/she navigate to settings. Just curious to know that in what case you require a variable to decrement when it is killed by OS? I am not getting your use case. – Shobhit Puri Aug 20 '13 at 06:12
-
I have to download large file, so I want to do it right after the apps start. So I'd just read an int variable, which would vary from 0 to 1. App downloads file when the int equals 0, then increment value. When the app is being closed the variable decrements and it equals 0 again, so the next time it's ran it download the file again. – ZZ 5 Aug 20 '13 at 10:11
-
1Much easier way is to do it from inside `onCreate` of the first activity that would be started. Start a AsyncTask from inside oncreate of first activity itself. This would insure that each time app is started, you get the data. Till that time you can also consider using progressbar etc. – Shobhit Puri Aug 20 '13 at 10:23
-
Well, I use service for that, I'm afraid that doing it inside onCreate will block the application, I'd rather do it in service so user wouldn't know that something happens – ZZ 5 Aug 21 '13 at 06:32
The question is what you mean "close"?
If you close all your Activities, the App-process might still be running. If you mean that the "close" is just closing all of your Activities. You might define a "count" for all opening Activities, you can store it in DB or SharePerference. I think you can do follow(dummy codes):
In your project, you should define BasicActivity:
public class BasicActivity extends Activity {
onCreate() {
mPreference.incActivityCount();//++
super.onCreate();
}
onDestory() {
mPreference.decActivityCount();//--
if( mPreference.getActivity() == 0 ) {
//All being opened Activities have been closed.
onAppHasNoUIs();
}
super.onDestory();
}
onAppHasNoUIs() {
//All being opened Activities have been closed.
}
}

- 7,064
- 8
- 40
- 46