code: public LifecycleListener createLifecycleListener() {
          return new LifecycleListener() {
  
              @Override
              public void onForegrounded(Context context) {
                  synchronized (lock) {
                      if (!isForegrounded) {
                          isForegrounded = true;
                          connectSocket();
                          if (receiver == null) {
                              receiver = new InternetConnectionReceiver(socketConnection);
                          }
                          context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
                      }
                      lock.notifyAll();
                  }
              }
  
              @Override
              public void onBackgrounded(Context context) {
                  synchronized (lock) {
                      if (isForegrounded) {
                          isForegrounded = false;
                          disconnectSocket();
                          if (receiver != null && !isForegrounded) {
                              context.unregisterReceiver(receiver);
                          }
                      }
                      lock.notifyAll();
                  }
              }
          };
      }
docs: creates application lifecycle and network connectivity callbacks.
  
  @return Application lifecycle and network connectivity callbacks.