DEV Community

Nor
Nor

Posted on

androidx.navigation:navigation-ui is causing increasing inflation time for each component (in Java xml project)?

@mark:

simply by removing
implementation 'androidx.navigation:navigation-ui:2.9.5'
EACH SINGLE ONE of my MaterialButton inflation time decreases from 4.5~6ms to 3.5~4ms.
I have no idea why.


this is a pure java xml project. no Kotlin, no Jetpack Compose.

this is a hello world level project.
no complex theme or logic.

I didnt even use the navigation-ui in my project, I just added the dependency in gradle.

im not talking about the time to transition from one page to another -- not inflation of a fragment.
im talking about the literal inflation time for EACH component -- each (custom) view. That sit inside an already inflated fragment.

            long startTime = System.nanoTime();
            View itemView = inflater.inflate(layoutRes, container, false);
            long bindingInflateDoneTime = System.nanoTime();
Enter fullscreen mode Exit fullscreen mode

as I switch back to my normal complex project.
I gets down from 5.5~7ms to 4.7~5.5ms.

Note: this is not a thorough test, nor a formal benchmark.


more sample code for a better context:

        for (int i = 0; i < count; i++) {
            long startTime = System.nanoTime();
            View itemView = inflater.inflate(layoutRes, container, false);
            long bindingInflateDoneTime = System.nanoTime();
            double timeMs = (bindingInflateDoneTime - startTime) / 1_000_000.0;
            times.add((long) (timeMs * 1_000_000)); // Store in nanoseconds for precision

            Log.d(TAG, "  Item " + (i + 1) + ": " + timeMs + " ms");
            container.addView(itemView);
            inflatedViews.add(itemView);
        }
Enter fullscreen mode Exit fullscreen mode

        Trace.beginSection(TAG + " - binding runtime inflate");
        long inflateStartTime = System.nanoTime();

        LayoutInflater inflater = LayoutInflater.from(context);
        if (viewVariant == VARIANT_TAB) {
            NavItemTabBinding binding = NavItemTabBinding.inflate(inflater, this, true);
            // iconView = binding.navItemIcon;
            // titleView = binding.navItemTitle;
            navButton = binding.navButton;
        } else if (viewVariant == VARIANT_CARD) {
            NavItemCardBinding binding = NavItemCardBinding.inflate(inflater, this, true);
            navButton = binding.navButton;
            // iconView = binding.navItemIcon;
            // titleView = binding.navItemTitle;
        } else if (viewVariant == VARIANT_TEXT_ONLY) {
            NavItemTextOnlyBinding binding = NavItemTextOnlyBinding.inflate(inflater, this, true);
            titleView = binding.navItemTitle;
        } else {
            Trace.endSection();
            throw new IllegalArgumentException("Invalid variant: " + viewVariant);
        }


        long afterActualInflateTime = System.nanoTime();
        Log.d(TAG, "|----- Binding.inflate() duration: " + ((afterActualInflateTime - inflateStartTime) / 1_000_000.0) + "ms");
        Trace.endSection();
Enter fullscreen mode Exit fullscreen mode

Top comments (0)