5

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?

PearsonArtPhoto
  • 38,970
  • 17
  • 111
  • 142
Ocelot
  • 1,733
  • 4
  • 29
  • 53

2 Answers2

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

Community
  • 1
  • 1
Blo
  • 11,903
  • 5
  • 45
  • 99