Android NDK development detailed explanation of Wear navigation

Android NDK development detailed explanation of Wear navigation


Note: This guide discusses using Android View. Consider Compose for Wear OS, a new declarative approach to building interfaces on wearables based on the latest Material guidelines. With Jetpack Compose, you can write better apps with less code.
Once you design separate screens for each user journey, you may have a few vertical screens or a single screen. Next, you need to decide how to design these screens so that they work together and how to enable navigation.

design

Make sure your app uses a shallow and linear hierarchy, as described in App Design Guidelines.

First, your app's launcher should open the most common user journeys. Carefully design each user journey to put the most important content at the top. For vertical containers, use the bottom to link to other, less common user journeys and settings.
Insert image description here

Figure 1. Make sure your most important content is at the top of your vertical container.

When a user enters one of your screens, make sure they can use the swipe to close gesture to navigate down the back stack.

Implement navigation

When implementing navigation, you have three options to choose from, as described in the following sections:

Use activities only (recommended approach)
activities and fragments
Jetpack Navigation

Only use activities

Since vertical screens are typically one depth deep, you can use an activity to implement all screens without using fragments.

We strongly recommend you use this method. It simplifies your code and the activity automatically supports sliding to close. Additionally, this approach will make implementing ambient sound patterns much simpler.

Note: If you are not using fragment, make sure to inherit the activity from ComponentActivity. Other activity types use mobile-specific interface elements that are not required by Wear OS.

activities and fragments

You can use fragments and activities together in your Wear OS apps. However, we don't recommend this because the advantages of using fragments to create shallow and flat architectures are not obvious.

Note: If you are using fragments, you should inherit them from FragmentActivity. Other activity types use mobile-specific interface elements that are not required by Wear OS.

Some of the issues you face using fragments in your Wear OS apps include:

You must implement the swipe to close gesture yourself. Otherwise, when the user performs the swipe gesture, the entire app will be exited.
If you are using AmbientMode, you must customize it for it to function properly. The AmbientMode is set for the activity, so you must take this into account when implementing the fragment.
To support sliding to close through a fragment, you must encapsulate the view containing the fragment in the SwipeDismissFrameLayout class. For more information, see Swipe to close gesture. Doing so provides users with a consistent app experience.

Note: When using fragments, use FragmentManager.add (not FragmentManager.replace) to support slide-to-close gestures. This helps ensure that the previous fragment is rendered below the top fragment when it is swiped to close.

Jetpack Navigation

Jetpack navigation works on Wear OS, but it has the same drawbacks as fragments. This approach results in increased development effort because Wear OS apps typically have shallow and linear hierarchies that don't provide many of the benefits. The "activity-only" approach is the best choice.

To get the most out of Jetpack navigation, do the following:

Make sure each fragment uses SwipeDismissFrameLayout as its root and manually use the close action to return to the navigation graph.
Implement a custom FragmentNavigator that can render fragments cascadingly.
The content and code examples on this page are subject to the license described in the Content License section. Java and OpenJDK are registered trademarks of Oracle and/or its affiliates.

Last updated (UTC): 2023-09-25.

Guess you like

Origin blog.csdn.net/hnjzfwy/article/details/134888903