Skip to content

Add overridable attachContent() to allow non-RIB roots in RibActivity#662

Merged
jbarr21 merged 1 commit into
uber:mainfrom
dipanshu281:overridable-attach-content
Jul 15, 2026
Merged

Add overridable attachContent() to allow non-RIB roots in RibActivity#662
jbarr21 merged 1 commit into
uber:mainfrom
dipanshu281:overridable-attach-content

Conversation

@dipanshu281

@dipanshu281 dipanshu281 commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

Summary

Currently there isn't a way to test RIB vs non-RIB architecture without creating two different activities to bypass RIB creation code in RibActivity. Two activities introduce additional latency on startup path. This PR decouples the RIB creation block from onCreate() so it can be no-oped in one of the branches for a test.

Change

  • Extract the root-attach block from onCreate into a new protected open fun attachContent(rootViewGroup, savedInstanceState). The default implementation reproduces the current inline logic, so behavior for existing subclasses is unchanged.
  • Drop the ?: throw NullPointerException("Router should not be null") in onSaveInstanceState. Because router is already declared nullable, the elvis-safe delegation is semantically identical when router != null (the common case); the null case now no-ops cleanly instead of NPE-ing when a subclass overrides attachContent to skip attach.
  • onDestroy and onBackPressed were already null-safe; no change.

Backward compatibility

  • API: additive. attachContent is a new protected open method with a default that reproduces the current inline logic.
  • Behavior: no change for any subclass that inherits the default attachContent.
  • onSaveInstanceState: identical behavior when router != null; the previously-guaranteed-then-thrown null path becomes a no-op.

Tests

Added RibActivityTest cases exercising a subclass that overrides attachContent to skip attach:

  • onCreate_whenAttachContentOverriddenToSkip_shouldLeaveRouterNullattachContent runs, createRouter is not invoked, and interactor access throws IllegalStateException.
  • onCreate_whenAttachContentOverriddenToSkip_shouldStillPublishLifecyclelifecycle() still emits CREATE.
  • onBackPressed_whenRouterIsNull_shouldFallThroughToSuperonUnhandledBackPressed() runs and the activity finishes.
  • onSaveInstanceState_whenRouterIsNull_shouldNotCrash — no NPE.

Full :libraries:rib-android:test suite passes locally (17/17 in RibActivityTest).

Extracts the root-attach block from onCreate into a new `protected open
attachContent(rootViewGroup, savedInstanceState)` method with the current
behavior as its default. Subclasses can override it to host an alternative
root (e.g. a Compose host) while still receiving RibActivity's lifecycle
publishing and saved-state wrapping.

Also drops the elvis-throw in onSaveInstanceState so a null router - now a
valid state when attachContent is overridden to skip attach - no longer
crashes the delegation. onDestroy and onBackPressed were already null-safe.

Adds RibActivityTest cases exercising an override that skips attach:
router stays null (interactor access throws), lifecycle CREATE still fires,
onBackPressed falls through to super, and onSaveInstanceState no longer
crashes.
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@jbarr21
jbarr21 requested review from Copilot and jbarr21 July 14, 2026 19:04
@jbarr21
jbarr21 requested review from jbarr21 and removed request for Copilot and jbarr21 July 14, 2026 19:06
@jbarr21
jbarr21 merged commit 4146875 into uber:main Jul 15, 2026
3 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants