Skip to content

Headless JS Android -- detachViewFromInstance() prevents Background Tasks if App is closed #16267

@trevorwhealy

Description

@trevorwhealy

When the app is closed, detachViewFromInstance is invoked by detachRootView and from then on, all the native BroadcastReceivers are disabled, meaning that background functionality and services are not truly possible with closed applications.

Background services are a major component of certain applications, and if the native Android behavior (the BroadcastReceivers) are severed as part of the application closing, then..

The headlessJS bridge is not useful if the "background service" only functions if the app is minimized, but not if it is truly closed.

ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManager.java

private void detachViewFromInstance(
      ReactRootView rootView,
      CatalystInstance catalystInstance) {
    Log.d(ReactConstants.TAG, "ReactInstanceManager.detachViewFromInstance()");
    UiThreadUtil.assertOnUiThread();
    catalystInstance.getJSModule(AppRegistry.class)
        .unmountApplicationComponentAtRootTag(rootView.getId());
  }

  ...

  /**
   * Detach given {@param rootView} from current catalyst instance. It's safe to call this method
   * multiple times on the same {@param rootView} - in that case view will be detached with the
   * first call.
   */
  @ThreadConfined(UI)
  public void detachRootView(ReactRootView rootView) {
    UiThreadUtil.assertOnUiThread();
    if (mAttachedRootViews.remove(rootView)) {
      ReactContext currentContext = getCurrentReactContext();
      if (currentContext != null && currentContext.hasActiveCatalystInstance()) {
        detachViewFromInstance(rootView, currentContext.getCatalystInstance());
      }
    }
  }

Is this a bug report?

Yes

Have you read the Contributing Guidelines?

Yes

Environment

react-native info

Environment:
OS: macOS Sierra 10.12.3
Node: 8.5.0
Yarn: Not Found
npm: 5.3.0
Watchman: 4.9.0
Xcode: Xcode 8.3.3 Build version 8E3004b
Android Studio: 2.3 AI-162.4069837

Packages: (wanted => installed)
react: 16.0.0-beta.5 => 16.0.0-beta.5
react-native: 0.49.2 => 0.49.2

Target Platform: Android Emulator - Nexus_6_API_23

Expected Behavior

The behavior in background mode would be consistent with behavior when the app is truly closed.

Actual Behavior

Logs come from background mode, but no logs come from the closed app.

Metadata

Metadata

Assignees

No one assigned

    Labels

    StaleThere has been a lack of activity on this issue and it may be closed soon.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions