I'm designing an architecture where the app needs to execute certain set of operations everytime it goes to background (onPause) and a set of operations everytime it comes back to foreground (onResume), irrespective of the activity (for all the activities). Is there a way with which I can achieve this, without having to call those methods in every activity class' onPause and onResume overrides?
Asked
Active
Viewed 1,797 times
2 Answers
9
Make your own class that extends Activity
and add your desired behavior to its onPause
and onResume
methods.
Then you extend that class on your activities.
public class BaseActivity extends Activity {
@Override
protected void onPause() {
// ...
}
@Override
protected void onResume() {
// ...
}
}
public class Activity1 extends BaseActivity {
// ...
}

Vitor M. Barbosa
- 3,286
- 1
- 24
- 36
-
This gets called whenever the user switches activities, how can we make it get called only if the user has left the app? – Ruchir Baronia Nov 11 '15 at 19:51
-
@Rich for that, you should override `onDestroy()` – Vitor M. Barbosa Nov 12 '15 at 12:39
1
You could extends your Activities
by a BaseActivity
which extends Activity
, and create the two methods onPause / onResume in it.
See this answer for more information.