Package 

Class UIManagerModule

  • All Implemented Interfaces:
    com.facebook.react.bridge.JSIModule , com.facebook.react.bridge.LifecycleEventListener , com.facebook.react.bridge.NativeModule , com.facebook.react.bridge.OnBatchCompleteListener , com.facebook.react.bridge.PerformanceCounter , com.facebook.react.bridge.UIManager

    
    public class UIManagerModule
    extends ReactContextBaseJavaModule implements OnBatchCompleteListener, LifecycleEventListener, UIManager
                        

    Native module to allow JS to create and update native Views.

    == Transactional Requirement ==A requirement of this class is to make sure that transactional UI updates occur all at once, meaning that no intermediate state is ever rendered to the screen. For example, if a JS application update changes the background of View A to blue and the width of View B to 100, both need to appear at once. Practically, this means that all UI update code related to a single transaction must be executed as a single code block on the UI thread. Executing as multiple code blocks could allow the platform UI system to interrupt and render a partial UI state.

    To facilitate this, this module enqueues operations that are then applied to native view hierarchy through NativeViewHierarchyManager at the end of each transaction.

    == CSSNodes ==In order to allow layout and measurement to occur on a non-UI thread, this module also operates on intermediate CSSNodeDEPRECATED objects that correspond to a native view. These CSSNodeDEPRECATED are able to calculate layout according to their styling rules, and then the resulting x/y/width/height of that layout is scheduled as an operation that will be applied to native view hierarchy at the end of current batch. TODO(5241856): Investigate memory usage of creating many small objects in UIManageModule and consider implementing a pool TODO(5483063): Don't dispatch the view hierarchy at the end of a batch if no UI changes occurred
    • Method Detail

      • initialize

         void initialize()

        This is called at the end of createCatalystInstance afterthe CatalystInstance has been created, in order to initialize NativeModules that require theCatalystInstance or JS modules.

      • onHostResume

         void onHostResume()

        Called either when the host activity receives a resume event (e.g. onResume orif the native module that implements this is initialized while the host activity is alreadyresumed. Always called for the most current activity.

      • onHostPause

         void onHostPause()

        Called when host activity receives pause event (e.g. onPause. Always calledfor the most current activity.

      • onHostDestroy

         void onHostDestroy()

        Called when host activity receives destroy event (e.g. onDestroy. Only calledfor the last React activity to be destroyed.

      • onCatalystInstanceDestroy

         void onCatalystInstanceDestroy()

        Allow NativeModule to clean up. Called before {CatalystInstance#onHostDestroy}

      • preInitializeViewManagers

        @Deprecated() void preInitializeViewManagers(List<String> viewManagerNames)

        Helper method to pre-compute the constants for a view manager. This method ensures that wedon't block for getting the constants for view managers during TTI

        Parameters:
        viewManagerNames - < names of ViewManagers
      • synchronouslyUpdateViewOnUIThread

         void synchronouslyUpdateViewOnUIThread(int tag, ReadableMap props)

        Used by native animated module to bypass the process of updating the values through the shadowview hierarchy. This method will directly update native views, which means that updates forlayout-related propertied won't be handled properly. Make sure you know what you're doingbefore calling this method :)

      • addRootView

         <T extends View> int addRootView(T rootView, WritableMap initialProps, @Nullable() String initialUITemplate)

        Registers a new root view. JS can use the returned tag with manageChildren to add/removechildren to this view.

        Calling addRootView through UIManagerModule calls addRootView in the non-Fabric renderer,always. This is deprecated in favor of calling startSurface in Fabric, which must be donedirectly through the FabricUIManager.

        Note that this must be called after getWidth()/getHeight() actually return something. SeeCatalystApplicationFragment as an example.

        TODO(6242243): Make addRootView thread safe NB: this method is horribly not-thread-safe.

      • startSurface

         <T extends View> int startSurface(T rootView, String moduleName, WritableMap initialProps, int widthMeasureSpec, int heightMeasureSpec)

        Registers a new root view with width and height.

      • stopSurface

         void stopSurface(int surfaceId)

        Stop a surface from running in JS and clears up native memory usage. Assumes that the nativeView hierarchy has already been cleaned up. Fabric-only.

      • removeRootView

         void removeRootView(int rootViewTag)

        Unregisters a new root view.

      • updateNodeSize

         void updateNodeSize(int nodeViewTag, int newWidth, int newHeight)
      • setViewLocalData

         void setViewLocalData(int tag, Object data)

        Sets local data for a shadow node corresponded with given tag. In some cases we need a way tospecify some environmental data to shadow node to improve layout (or do something similar), so {@code localData} serves these needs. For example, any stateful embedded native views maybenefit from this. Have in mind that this data is not supposed to interfere with the state ofthe shadow view. Please respect one-directional data flow of React.

      • manageChildren

         void manageChildren(int viewTag, @Nullable() ReadableArray moveFrom, @Nullable() ReadableArray moveTo, @Nullable() ReadableArray addChildTags, @Nullable() ReadableArray addAtIndices, @Nullable() ReadableArray removeFrom)

        Interface for adding/removing/moving views within a parent view from JS.

        Parameters:
        viewTag - the view tag of the parent view
        moveFrom - a list of indices in the parent view to move views from
        moveTo - parallel to moveFrom, a list of indices in the parent view to move views to
        addChildTags - a list of tags of views to add to the parent
        addAtIndices - parallel to addChildTags, a list of indices to insert those children at
        removeFrom - a list of indices of views to permanently remove.
      • setChildren

         void setChildren(int viewTag, ReadableArray childrenTags)

        Interface for fast tracking the initial adding of views. Children view tags are assumed to bein order

        Parameters:
        viewTag - the view tag of the parent view
        childrenTags - An array of tags to add to the parent in order
      • replaceExistingNonRootView

        @Deprecated() void replaceExistingNonRootView(int oldTag, int newTag)

        Replaces the View specified by oldTag with the View specified by newTag within oldTag's parent.This resolves to a simple manageChildren call, but React doesn't have enough info inJS to formulate it itself.

      • removeSubviewsFromContainerWithID

        @Deprecated() void removeSubviewsFromContainerWithID(int containerTag)

        Method which takes a container tag and then releases all subviews for that container uponreceipt.

        Parameters:
        containerTag - the tag of the container for which the subviews must be removed
      • measure

         void measure(int reactTag, Callback callback)

        Determines the location on screen, width, and height of the given view and returns the valuesvia an async callback.

      • measureInWindow

         void measureInWindow(int reactTag, Callback callback)

        Determines the location on screen, width, and height of the given view relative to the devicescreen and returns the values via an async callback. This is the absolute position includingthings like the status bar

      • measureLayout

         void measureLayout(int tag, int ancestorTag, Callback errorCallback, Callback successCallback)

        Measures the view specified by tag relative to the given ancestorTag. This means that thereturned x, y are relative to the origin x, y of the ancestor view. Results are stored in thegiven outputBuffer. We allow ancestor view and measured view to be the same, in which case theposition always will be (0, 0) and method will only measure the view dimensions.

        NB: Unlike measure, this will measure relative to the view layout, not the visiblewindow which can cause unexpected results when measuring relative to things like ScrollViewsthat can have offset content on the screen.

      • measureLayoutRelativeToParent

        @Deprecated() void measureLayoutRelativeToParent(int tag, Callback errorCallback, Callback successCallback)

        Like measure and measureLayout but measures relative to the immediate parent.

        NB: Unlike measure, this will measure relative to the view layout, not the visiblewindow which can cause unexpected results when measuring relative to things like ScrollViewsthat can have offset content on the screen.

      • findSubviewIn

         void findSubviewIn(int reactTag, ReadableArray point, Callback callback)

        Find the touch target child native view in the supplied root view hierarchy, given a reacttarget location.

        This method is currently used only by Element Inspector DevTool.

        Parameters:
        reactTag - the tag of the root view to traverse
        point - an array containing both X and Y target location
        callback - will be called if with the identified child view react ID, and measurementinfo.
      • viewIsDescendantOf

        @Deprecated() void viewIsDescendantOf(int reactTag, int ancestorReactTag, Callback callback)

        Check if the first shadow node is the descendant of the second shadow node

      • setJSResponder

         void setJSResponder(int reactTag, boolean blockNativeResponder)
      • dispatchCommand

         void dispatchCommand(int reactTag, String commandId, @Nullable() ReadableArray commandArgs)

        Dispatches the commandId received by parameter to the view associated with the reactTag. Thecommand will be processed in the UIThread.

        Pre-Fabric, this is only called on the Native Module Thread.

        Parameters:
        reactTag - that identifies the view that will receive this command
        commandId - String command id
        commandArgs - ReadableArray parameters associated with the command
      • showPopupMenu

         void showPopupMenu(int reactTag, ReadableArray items, Callback error, Callback success)

        Show a PopupMenu.

        Parameters:
        reactTag - the tag of the anchor view (the PopupMenu is displayed next to this view); thisneeds to be the tag of a native view (shadow views can not be anchors)
        items - the menu items as an array of strings
        error - will be called if there is an error displaying the menu
        success - will be called with the position of the selected item as the first argument, orno arguments if the menu is dismissed
      • setLayoutAnimationEnabledExperimental

         void setLayoutAnimationEnabledExperimental(boolean enabled)

        LayoutAnimation API on Android is currently experimental. Therefore, it needs to be enabledexplicitly in order to avoid regression in existing application written for iOS using this API.

        Warning : This method will be removed in future version of React Native, and layoutanimation will be enabled by default, so always check for its existence before invoking it.

        TODO(9139831) : remove this method once layout animation is fully stable.

        Parameters:
        enabled - whether layout animation is enabled or not
      • configureNextLayoutAnimation

         void configureNextLayoutAnimation(ReadableMap config, Callback success, Callback error)

        Configure an animation to be used for the native layout changes, and native views creation. Theanimation will only apply during the current batch operations.

        TODO(7728153) : animating view deletion is currently not supported.

        Parameters:
        config - the configuration of the animation for view addition/removal/update.
        success - will be called when the animation completes, or when the animation getinterrupted.
        error - will be called if there was an error processing the animation
      • onBatchComplete

         void onBatchComplete()

        To implement the transactional requirement mentioned in the class javadoc, we only commit UIchanges to the actual view hierarchy once a batch of JS->Java calls have been completed. Weknow this is safe because all JS->Java calls that are triggered by a Java->JS call (e.g. thedelivery of a touch event or execution of 'renderApplication') end up in a single JS->Javatransaction.

        A better way to do this would be to have JS explicitly signal to this module when a UItransaction is done. Right now, though, this is how iOS does it, and we should probably updatethe JS and native code and make this change at the same time.

        TODO(5279396): Make JS UI library explicitly notify the native UI module of the end of a UItransaction using a standard native call

      • sendAccessibilityEvent

         void sendAccessibilityEvent(int tag, int eventType)

        Dispatch an accessibility event to a view asynchronously.

        Pre-Fabric, this is only called on the Native Module Thread.

      • addUIBlock

         void addUIBlock(UIBlock block)

        Schedule a block to be executed on the UI thread. Useful if you need to execute view logicafter all currently queued view updates have completed.

        Parameters:
        block - that contains UI logic you want to execute.
      • prependUIBlock

         void prependUIBlock(UIBlock block)

        Schedule a block to be executed on the UI thread. Useful if you need to execute view logicbefore all currently queued view updates have completed.

        Parameters:
        block - that contains UI logic you want to execute.
      • resolveRootTagFromReactTag

        @Deprecated() int resolveRootTagFromReactTag(int reactTag)

        Given a reactTag from a component, find its root node tag, if possible. Otherwise, this willreturn 0. If the reactTag belongs to a root node, this will return the same reactTag.

        Parameters:
        reactTag - the component tag
      • invalidateNodeLayout

         void invalidateNodeLayout(int tag)

        Dirties the node associated with the given react tag

      • updateRootLayoutSpecs

         void updateRootLayoutSpecs(int rootViewTag, int widthMeasureSpec, int heightMeasureSpec, int offsetX, int offsetY)

        Updates the styles of the ReactShadowNode based on the Measure specs received byparameters. offsetX and offsetY aren't used in non-Fabric, so they're ignored here.

      • resolveView

         View resolveView(int tag)

        Resolves a view based on its reactTag. Do not mutate properties on this view that are alreadymanaged by React, as there are no guarantees this changes will be preserved.

      • receiveEvent

         void receiveEvent(int reactTag, String eventName, @Nullable() WritableMap event)

        This method dispatches events from RN Android code to JS. The delivery of this event will notbe queued in EventDispatcher class.

        Parameters:
        reactTag - tag
        eventName - name of the event
        event - parameters
      • receiveEvent

         void receiveEvent(int surfaceId, int reactTag, String eventName, @Nullable() WritableMap event)

        This method dispatches events from RN Android code to JS. The delivery of this event will notbe queued in EventDispatcher class.

        Parameters:
        reactTag - tag
        eventName - name of the event
        event - parameters