目录

Navigation FAQs

Dialog-Type NavDestination Mask Animation Is Not Smooth

Symptom

When the default transition animation of NavDestinationMode.DIALOG is used, the following two issues occur:

It is expected that the mask fades out while the content area exits downward.

Solution

Execute the background color animation in the onWillAppear and onWillDisappear lifecycle callbacks. The following is an example:

@Builder
export function DialogNavDestinationBuilder() {
  DialogNavDestination();
}

@Component
export struct DialogNavDestination {
  stack: NavPathStack = AppStorage.get<NavPathStack>('basicNavigationStack')!;
  @State backColor: ResourceColor = '#00000000';

  build() {
    NavDestination() {
      Stack() {
        Text('Dialog')
          .fontSize(44)
          .backgroundColor(Color.White)
      }
      .width('100%')
      .height('100%')
    }
    .hideTitleBar(true)
    .backgroundColor(this.backColor)
    .mode(NavDestinationMode.DIALOG)
    .onWillAppear(() => {
      // Fade in the mask on startup.
      this.getUIContext().animateTo({ duration:450 }, () => {
        this.backColor = '#66000000';
      });
    })
    .onWillDisappear(() => {
      // Fade out the mask on disappearance.
      this.getUIContext().animateTo({ duration: 450 }, () => {
        this.backColor = '#00000000';
      });
    })
  }
}

Router and Navigation Animation Conflict

Symptom

When router navigates to a navigation page, and navigation immediately pushes a NavDestination page in the aboutToAppear callback, the Page and the NavDestination page display animations at the same time, resulting in a poor effect.

Solution

Disable the animation of the push in aboutToAppear:

@Entry
@Component
struct NavigationPage {
  navStack: NavPathStack = new NavPathStack();

  aboutToAppear(): void {
    AppStorage.setOrCreate<NavPathStack>('basicNavigationStack', this.navStack);
    this.navStack.pushPath({ name: 'animation-BasicNavDestination' }, false); // Disable the animation for this push.
  }

  build() {
    Navigation(this.navStack) {
      // ...
    }
  }
}

Pop and Push Performed Simultaneously but Pop Animation Executed

Symptom

When you pop the top page of the stack and then immediately push a page, the animation effect is the pop animation of the top page, not the push animation of PageOne.

this.stack.pop();
this.stack.pushPath({ name: 'animation-BasicNavDestination' });

Solution

In a single operation, regardless of how many times the pop and push APIs are called, Navigation calculates the final result and performs only one animation.

The page transition animation of the current Navigation is determined primarily by the top page of the stack before the operation. If the top page is no longer in the stack after the operation, the pop animation is executed; if it is still in the stack, the push animation is executed.

For example, suppose the current top page is PageTop. If you first call pop to remove PageTop and then call push to add PageNew to the stack, PageTop is no longer in the stack after the operation, so the animation finally executed is the pop animation.

If you want to remove a page while pushing another page and execute the push animation, you can set the pushed page to NEW_INSTANCE, which executes the push animation by default:

this.stack.pop();
this.stack.pushPath({ name: 'animation-BasicNavDestination' }, { launchMode: LaunchMode.NEW_INSTANCE });

Is There an End Callback for the Transition Animation?

The current system animation does not provide an animation end callback. Only custom transition animations provide an end callback. You need to implement custom transition animation yourself. Related APIs: NavDestinationTransition, NavigationAnimatedTransition.

How to Implement a Shared Element Transition Between Navigation and NavDestination

Currently, only transitions between NavDestination components support the shared element transition effect. The system does not yet support the shared element effect for transitions between NavBar and NavDestination.

The shared element transition of NavDestination must be implemented together with the geometryTransition API, and the following conditions must be met:

For a sample, see Shared Element Transition.

Page Is Blank When Multiple if Statements Are Used in the NavDestination Builder

Symptom

When multiple independent if statements are used in the builder specified by the navDestination property of Navigation to build NavDestination pages separately, some pages appear blank after navigation. For example, in the following usage, the page is blank when name is PageC:

@Entry
@Component
struct ExamplePage {
  private stack: NavPathStack = new NavPathStack();

  @Builder
  pageMap(name: string) {
    if (name === 'PageA') {
      NavDestination()
        .backgroundColor(Color.Red)
        .title('PageA')
    }
    if (name === 'PageB') {
      NavDestination()
        .backgroundColor(Color.Green)
        .title('PageB')
    }
    if (name === 'PageC') {
      NavDestination()
        .backgroundColor(Color.Blue)
        .title('PageC')
    }
  }

  build() {
    Navigation(this.stack) {
      Button("push PageA").onClick((event: ClickEvent) => {
        this.stack.pushPathByName("PageA", null)
      })
      Button("push PageB").onClick((event: ClickEvent) => {
        this.stack.pushPathByName("PageB", null)
      })
      Button("push PageC").onClick((event: ClickEvent) => {
        this.stack.pushPathByName("PageC", null)
      })
    }
    .navDestination(this.pageMap)
  }
}

Cause Analysis

The mechanism by which Navigation builds NavDestination is as follows:

  1. After the builder is executed, the first node serves as the root node, and the remaining nodes are mounted under this root node in code order. (Nodes that are siblings of the first node are mounted as child nodes.)
  2. When Navigation searches for the NavDestination node, it traverses downward from the root node along the first child node of each level until it finds the NavDestination. If the first child node at a certain level has no child nodes, the traversal terminates and cannot continue to search subsequent sibling nodes.

Each independent if statement produces an IfElseNode. The second and third IfElseNode nodes are mounted as sibling child nodes of the first IfElseNode, and the NavDestination is mounted under the IfElseNode whose condition is true.

Solution

Change the multiple independent if statements in the builder to an if-else if-else judgment, which produces only one IfElseNode as the root node, ensuring that Navigation can traverse to find the NavDestination:

@Entry
@Component
struct NavDestinationSystemTransition {
  // ...

  @Builder
  pageMap(name: string) {
    if (name === 'Fade') {
      Fade();
    } else if (name === 'Explode') {
      Explode();
    } else if (name === 'SlideRight') {
      SlideRight();
    } else if (name === 'SlideBottom') {
      SlideBottom();
    } else {
      Dest();
    }
  }

  aboutToAppear(): void {
    this.stack.pushPath({name: 'Dest'});
  }

  build() {
    Navigation(this.stack) {
      // ...
    }
    .navDestination(this.pageMap)
    .hideNavBar(true)
  }
}