This specification is maintained by the AgentRules Working Group. Have enhancements or want to claim maintainership?Submit Specification →
mobilecritical PriorityID: android-15-predictive-back-gesture-state-reconciliation

Android 15 Predictive Back Gesture Navigation State Restoration Invariant

Enforces deterministic reconciliation of Fragment view hierarchies and navigation backstack states across predictive back gesture lifecycles in Android 15 (API 35). Prevents state desynchronization, phantom backstack pops, and orphaned view properties when interactive swipe-to-back gestures are canceled or interrupted mid-progress. Mandates strict separation between transient gesture-driven visual transformations and non-reversible navigation commits.

Prohibited Anti-Pattern: Mutating Navigation State, Committing FragmentTransactions, or Failing to Revert View Interpolations During Canceled Predictive Back Progressions

Export Ready-to-Drop Configuration

Select format below to copy code or download file directly.

---
description: Android 15 Predictive Back Gesture Navigation State Restoration Invariant - Enforces deterministic reconciliation of Fragment view hierarchies and navigation backstack states across predictive back gesture lifecycles in Android 15 (API 35). Prevents state desynchronization, phantom backstack pops, and orphaned view properties when interactive swipe-to-back gestures are canceled or interrupted mid-progress. Mandates strict separation between transient gesture-driven visual transformations and non-reversible navigation commits.
globs: app/src/main/java/**/*.kt, app/src/main/kotlin/**/*.kt, feature/**/src/main/java/**/*.kt, core/navigation/src/main/java/**/*.kt
alwaysApply: true
verified_by: "agentrules.org"
license: "CC-BY-4.0"
source: "https://agentrules.org/rules/android-15-predictive-back-gesture-state-reconciliation"
---

# Android 15 Predictive Back Gesture Navigation State Restoration Invariant

> **Severity**: CRITICAL | **Category**: mobile
> **Anti-Pattern Warning**: Mutating Navigation State, Committing FragmentTransactions, or Failing to Revert View Interpolations During Canceled Predictive Back Progressions

## System Constraints
You are a Principal Mobile Architect enforcing Android 15 Predictive Back Gesture invariants. You must prohibit premature state commits, irreversible side effects in progress callbacks, and unhandled gesture cancellations in Fragment and Navigation architectures. Mandate exact lifecycle matching across handleOnBackStarted, handleOnBackProgressed, handleOnBackCancelled, and handleOnBackPressed.

## Strict Guidelines
- Engineers MUST ALWAYS implement OnBackPressedCallback using handleOnBackStarted, handleOnBackProgressed, handleOnBackCancelled, and handleOnBackPressed to manage four-stage gesture lifecycles.
- Engineers MUST NOT execute FragmentTransaction, NavController.popBackStack(), or mutate ViewModel navigational state within handleOnBackStarted or handleOnBackProgressed.
- Code MUST strictly restrict handleOnBackProgressed to pure, idempotent visual interpolations (e.g., translationX, scale, alpha) calculated directly from BackEventCompat.progress.
- Code MUST restore all modified view transformation properties to their default baseline values (translationX = 0f, scale = 1f, alpha = 1f) inside handleOnBackCancelled.
- Components NEVER assume handleOnBackStarted will culminate in handleOnBackPressed; all resource allocations or temporary animation locks created in started/progressed MUST be disposed cleanly in handleOnBackCancelled.
- Services and ViewModels MUST NEVER register persistent database or network writes triggered directly by transient back gesture events.

## Bad vs Good Implementations
### Predictive Back Callback State Reconciliation (Kotlin)

#### Bad Antipattern (AI Common Hallucination)
> **Why it fails**: Mutating ViewModel domain state and triggering NavController.popBackStack() inside progress interpolation breaks predictive back invariants. If the user cancels the gesture mid-swipe, the navigation state is desynchronized, leaving the Fragment destroyed or orphaned with mutated domain state.
```kotlin
class CheckoutFragment : Fragment(R.layout.fragment_checkout) {
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        
        requireActivity().onBackPressedDispatcher.addCallback(
            viewLifecycleOwner,
            object : OnBackPressedCallback(true) {
                override fun handleOnBackPressed() {
                    findNavController().popBackStack()
                }
                
                // VIOLATION: Mutating business state and committing irreversible UI changes
                // during progressive gesture input, with no cancellation handler.
                fun handleOnBackProgressed(backEvent: BackEventCompat) {
                    view.alpha = 1f - backEvent.progress
                    if (backEvent.progress > 0.5f) {
                        // Catastrophic: triggers navigation commit before gesture completion
                        viewModel.cancelOrderDraft()
                        findNavController().popBackStack()
                    }
                }
            }
        )
    }
}
```

#### Production Standard (Correct)
> **Standard**: Adheres strictly to Android 15 predictive back invariants. Gesture progress is limited strictly to ephemeral visual interpolations. When the gesture is aborted, handleOnBackCancelled reverts visual state smoothly to initial baselines, avoiding premature navigation commits or corrupted backstacks.
```kotlin
class CheckoutFragment : Fragment(R.layout.fragment_checkout) {
    private var _binding: FragmentCheckoutBinding? = null
    private val binding get() = _binding!!

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        _binding = FragmentCheckoutBinding.bind(view)

        val predictiveBackCallback = object : OnBackPressedCallback(true) {
            override fun handleOnBackStarted(backEvent: BackEventCompat) {
                // Initialize transient animation state; zero navigation mutations
            }

            override fun handleOnBackProgressed(backEvent: BackEventCompat) {
                val progress = backEvent.progress
                val interpolatedScale = 1f - (progress * 0.1f)
                binding.rootContainer.scaleX = interpolatedScale
                binding.rootContainer.scaleY = interpolatedScale
                binding.rootContainer.alpha = 1f - (progress * 0.3f)
            }

            override fun handleOnBackPressed() {
                // Final deterministic navigation commit
                findNavController().popBackStack()
            }

            override fun handleOnBackCancelled() {
                // Invariant: Reset view hierarchy transformations deterministically
                binding.rootContainer.animate()
                    .scaleX(1.0f)
                    .scaleY(1.0f)
                    .alpha(1.0f)
                    .setDuration(150L)
                    .start()
            }
        }

        requireActivity().onBackPressedDispatcher.addCallback(
            viewLifecycleOwner,
            predictiveBackCallback
        )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}
```

Architectural Specification & Invariant Rationale

Architectural Invariant Design: Android 15 Predictive Back Gesture Navigation State Restoration Invariant

Android 15 Predictive Back Gesture Navigation State Restoration Invariant defines an industrial-grade behavioral standard for mobile engineering. Enforces deterministic reconciliation of Fragment view hierarchies and navigation backstack states across predictive back gesture lifecycles in Android 15 (API 35). Prevents state desynchronization, phantom backstack pops, and orphaned view properties when interactive swipe-to-back gestures are canceled or interrupted mid-progress. Mandates strict separation between transient gesture-driven visual transformations and non-reversible navigation commits. Modern software architectures require strict adherence to deterministic patterns to guarantee high availability, maintainability, and zero security vulnerabilities.

Framework Paradigm Shift & Drift MechanicsLLMs are trained on millions of open-source repositories containing outdated code, legacy idioms, and unpatched antipatterns. When coding assistants generate implementations for this stack, they default to common statistical tokens: Mutating Navigation State, Committing FragmentTransactions, or Failing to Revert View Interpolations During Canceled Predictive Back Progressions Without an explicit system instruction file, developers spend hours debugging silent failures.
Failure Modes & Production Blast Radius

Hazard Analysis: What Breaks Without This Rule

The following failure modes have been cataloged from hundreds of hours of production code reviews where AI assistants produced non-compliant implementations:

Failure ModeAI Symptom (The Anti-Pattern)SeverityProduction Impact & Remedy
AI Hallucination & Deprecation DriftMutating Navigation State, Committing FragmentTransactions, or Failing to Revert View Interpolations During Canceled Predictive Back ProgressionsCritical
Violating this industrial standard introduces regression bugs, security risks, and unexpected runtime failures in production.
Enforce explicit behavioral system directives across all AI coding assistants in your repository.
Silent Architecture InconsistencyAI assistants produce mixed paradigms across files, creating fragmented codebase conventions.Medium
Increases cognitive load during code reviews and leads to maintenance debt across engineering teams.
Pin project conventions in .cursor/rules, CLAUDE.md, and AGENTS.md so all team agents follow identical guardrails.
Architectural Directives in Detail

Step-by-Step Production Directives

Each rule constraint below is injected into the AI agent's system prompt to establish deterministic boundaries:

1

Engineers MUST ALWAYS implement OnBackPressedCallback using handleOnBackStarted, handleOnBackProgressed, handleOnBackCancelled, and handleOnBackPressed to manage four-stage gesture lifecycles.

Engineers MUST ALWAYS implement OnBackPressedCallback using handleOnBackStarted, handleOnBackProgressed, handleOnBackCancelled, and handleOnBackPressed to manage four-stage gesture lifecycles.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
2

Engineers MUST NOT execute FragmentTransaction, NavController.popBackStack(), or mutate ViewModel navigational state within handleOnBackStarted or handleOnBackProgressed.

Engineers MUST NOT execute FragmentTransaction, NavController.popBackStack(), or mutate ViewModel navigational state within handleOnBackStarted or handleOnBackProgressed.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
3

Code MUST strictly restrict handleOnBackProgressed to pure, idempotent visual interpolations (e.g., translationX, scale, alpha) calculated directly from BackEventCompat.progress.

Code MUST strictly restrict handleOnBackProgressed to pure, idempotent visual interpolations (e.g., translationX, scale, alpha) calculated directly from BackEventCompat.progress.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
4

Code MUST restore all modified view transformation properties to their default baseline values (translationX = 0f, scale = 1f, alpha = 1f) inside handleOnBackCancelled.

Code MUST restore all modified view transformation properties to their default baseline values (translationX = 0f, scale = 1f, alpha = 1f) inside handleOnBackCancelled.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
5

Components NEVER assume handleOnBackStarted will culminate in handleOnBackPressed

Components NEVER assume handleOnBackStarted will culminate in handleOnBackPressed; all resource allocations or temporary animation locks created in started/progressed MUST be disposed cleanly in handleOnBackCancelled.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
6

Services and ViewModels MUST NEVER register persistent database or network writes triggered directly by transient back gesture events.

Services and ViewModels MUST NEVER register persistent database or network writes triggered directly by transient back gesture events.

Rationale:AI models frequently generate unconstrained code here because statistical language distributions lack project-specific architectural context. Mandating this directive ensures that generated code adheres to verified industrial production requirements.
Enforcement:Integrated into the Agent Pack rule file. When Cursor, Claude Code, or Codex processes this prompt, it applies this constraint before emitting syntax.
Technical Questions & Answers

Frequently Asked Technical Questions

Common questions developers face when configuring AI coding assistants for this stack:

Q:Why is Android 15 Predictive Back Gesture Navigation State Restoration Invariant critical for AI coding assistants?

Enforces deterministic reconciliation of Fragment view hierarchies and navigation backstack states across predictive back gesture lifecycles in Android 15 (API 35). Prevents state desynchronization, phantom backstack pops, and orphaned view properties when interactive swipe-to-back gestures are canceled or interrupted mid-progress. Mandates strict separation between transient gesture-driven visual transformations and non-reversible navigation commits. Without explicit behavioral boundaries, AI coding assistants (Cursor, Claude Code, Windsurf, Codex) rely on training data averages, which frequently produce deprecated, inefficient, or vulnerable code patterns.

Q:How does adopting this rule prevent AI hallucinations?

This rule explicitly declares what the AI must avoid (Mutating Navigation State, Committing FragmentTransactions, or Failing to Revert View Interpolations During Canceled Predictive Back Progressions) and prescribes verified positive examples. Grounding the LLM with negative and positive pairs eliminates guesswork.

Q:Which AI coding environments are supported?

This specification is multi-platform compatible: Cursor (.cursor/rules/*.mdc & .cursorrules), Anthropic Claude Code (CLAUDE.md), Windsurf (.windsurfrules), Codex / Cline (AGENTS.md), and GitHub Copilot (.github/copilot-instructions.md).

Q:Can I combine this rule with other technology stacks?

Yes. Use the Interactive Stack Builder or download an Agent Pack to merge this rule with other frontend, backend, database, security, and DevOps rules without syntax collision.

Open Specification · CC BY 4.0·Free for Personal, Team & Commercial In-Repo Use

Citation & Open Source Attribution Guidelines

This specification is licensed under Creative Commons Attribution 4.0 International (CC BY 4.0). You are completely free to adapt and share this rule in your workspace. If citing, including in public AI benchmark datasets, or redistributing, attribution to AgentRules.org is required.

Related MOBILE Rules

View all rules →
1 Rule in Stack

Android