Fix race when navigating Back before hydration (#96252)
Relands #95682 (reverted in #95853 due to #95848).
This contains:
- A bunch of tests (some failing and some passing before this PR; all
passing after this PR).
- The fix from #95682.
In short, we use Navigation API to detect during hydration that we're on
a different history entry than we started, and in that case, replay the
missed traversal with similar logic to what we do for `onPopState`.
The issue with the original PR was that under Activity, React would
sometimes hang applying the state update. This was fixed in
https://github.com/react/react/pull/37135 and synced, so we can do this
fix again now.
## Test Plan
Old and new tests.