MG Backgrounded - mrkcsc/android-mg-bootstrap GitHub Wiki

Configuration

Initialize the backgrounded utility in your Application class:


class App extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        
        MGBackgrounded.getConfig().init(this);
    }
}

Usage

Once initialized you can query for back-grounded state in a blocking or non-blocking fashion:


// Returns true or false.
MGBackgrounded.isBackgrounded()

// RxJava observable.
MGBackgrounded.get().subscribe(backgrounded -> {
            
});

Intended Usage

Generally you might want to use back-grounded state to determine things like:

  • How to handle GCM notifications
  • Shutting down long living connections.
  • Pausing audio.

Caveats

An android application is considered in the background if an Activity calls onPause and another activity of that Application does not get resumed shortly after (threshold is set to one second). Similarly an Activity is not considered to be in the foreground until the onResume event of an activity is called.