When user goes to other apps from my app or presses device home button from my app or etc , then my app will be sent to background. And in the background am doing some stuff . So how to know is my app has gone to background . One solution will be to check if any of app activities are in onstart,onrestart,onresume,onpause,onstop if not then we can consider it in background. But this solution is tedious please help me if you have a easy solution
Asked
Active
Viewed 278 times
0
-
1What is tedious about `onPause`? I think it is a direct and plain approach to get what you want. – Howard Jan 15 '13 at 06:51
-
@Howard for exch activity i have to keep track whether they belong to any of the states or not , if my app has say 15 activities then the changes need to made on all of them . SO was looking something easy stuff like any API telling me that my app has gone to background or some thing like that. – Basavaraj Hampali Jan 15 '13 at 07:11
-
this is already asked and a very good answer by FunkTheMonk http://stackoverflow.com/questions/8358155/detect-when-android-app-go-backrgound, you'll need to change code according to your need, Thats it. – QAMAR Jan 15 '13 at 08:10
1 Answers
2
Using onPause
isn't tedious, and may be your only solution anyway. Create an abstract Activity
class that all your Activity
classes extend and do whatever you need to do in onPause
there:
public abstract class BaseActivity extends Activity {
@Override
public void onPause() {
super.onPause();
// Do whatever you need to do in onPause here
}
}
Using this example, all your Activity
classes should now extend BaseActivity
.

Jason Robinson
- 31,005
- 19
- 77
- 131
-
What do you say abt this one http://stackoverflow.com/questions/8358155/detect-when-android-app-go-backrgound . Is it a clean way.Thanks – Basavaraj Hampali Jan 15 '13 at 08:53
-
He edited his answer 2 hours after you commented here. He wrote "UPDATE: getRunningTasks has been declared to not be guaranteed to be accurate." – Jason Robinson Jan 15 '13 at 22:53