ナビゲーション マップ操作のベスト プラクティス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
このページでは、アプリでナビゲーション マップを操作するためのベスト プラクティスについて説明します。
可能な限り NavigationView
ではなく SupportNavigationFragment
を使用する
SupportNavigationFragment
は NavigationView
ライフサイクル コールバックを簡単に処理するためのラッパーです。そのため、これらのコールバックを自分で管理する必要はありません。この方法はエラーが発生しにくく、アプリで Navigation を使用する際の推奨される方法です。SupportNavigationFragment
を使用する場合は、NavigationView
ライフサイクル イベントを呼び出さないようにしてください。
NavigationView
を使用している場合は、ライフサイクル メソッドを呼び出すときに厳密な順序付けを使用する
NavigationView
はナビゲーション マップをホストし、Android のアクティビティやフラグメントと同様にライフサイクル イベントに密接に追従します。これらのライフサイクル イベントが呼び出されると、特定のアクションを実行します。NavigationView
は、NavigationView#onCreate
と NavigationView#onStart
で複数の初期化を実行し、NavigationView#onStop
と NavigationView#onDestroy
でクリーンアップを実行します。また、他のライフサイクル イベントが処理されるときにも実行します。
NavigationView
ライフサイクル メソッドは、Android アクティビティやフラグメントの場合と同じ意味を持ちます。たとえば、NavigationView
の onCreate()
は、Android アクティビティまたはフラグメントのライフサイクル コールバックによって呼び出されるように変換されます。NavigationView
ライフサイクル コールバックは Android ライフサイクル コールバックと同じ順序で呼び出されるため、これらの NavigationView メソッドの厳密な順序付けが必要です。そうしないと、メモリリーク、UI エラー、位置情報の更新がされないなどの問題が発生する可能性があります。
Android アクティビティのライフサイクルについて詳しくは、Android デベロッパー向けドキュメントのアクティビティのライフサイクルのコンセプトのセクションをご覧ください。
次の表は、指定されたライフサイクル メソッドの後に、他のライフサイクル メソッドを呼び出すタイミングを示しています。
ライフサイクル メソッド |
アクティビティのライフサイクルのどこで呼び出されるか |
どのライフサイクル メソッドの後に呼び出されるか |
onConfigurationChanged()
|
UI がフォアグラウンドにあり、構成が変更されたときに呼び出されます。 |
常に onStart() より後
|
onTrimMemory()
|
アクティビティがバックグラウンドにあるときに呼び出されます。 |
常に onPause() より後
|
onSaveInstance()
|
アクティビティが破棄される前に呼び出されます。 |
常に onStop() より後
|
対応する終了メソッドを最初に呼び出さずに、これらのライフサイクル メソッドを複数回呼び出さないでください。また、これらの Android ライフサイクル コールバックの一部がアプリ自体で管理され、NavigationView
が作成後または開始後にフラグメントに追加される場合は、アプリが特定のメソッドを適切な順序で呼び出して、Navigation SDK を正しく初期化する必要があります。
これらのメソッドの使用に関する追加のガイダンスについては、Navigation SDK デモアプリをご覧ください。
NavigationView
を使用する場合は、アクティビティまたはフラグメントのいずれかからライフサイクル イベントを呼び出す
ライフサイクル メソッドの厳密な順序を維持するには、アクティビティまたはフラグメントのライフサイクル コールバックからこれらのイベントを呼び出します。これらのイベントは順に受信されます。このアプローチにより、アプリはフラグメントとアクティビティの間で調整する必要がなくなり、呼び出しの重複を防ぐことができます。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-09-04 UTC。
[null,null,["最終更新日 2025-09-04 UTC。"],[[["\u003cp\u003ePrioritize using \u003ccode\u003eSupportNavigationFragment\u003c/code\u003e over \u003ccode\u003eNavigationView\u003c/code\u003e for simplified lifecycle management and error reduction.\u003c/p\u003e\n"],["\u003cp\u003eWhen using \u003ccode\u003eNavigationView\u003c/code\u003e directly, ensure strict adherence to the Android lifecycle order when invoking its lifecycle methods to prevent issues like memory leaks and UI errors.\u003c/p\u003e\n"],["\u003cp\u003eInvoke \u003ccode\u003eNavigationView\u003c/code\u003e lifecycle events exclusively from either the activity or the fragment to maintain order and avoid duplicate calls.\u003c/p\u003e\n"]]],[],null,["# Navigation map interaction best practices\n\nThis page explains best practices for interacting with the Navigation map in\nyour app.\n\nUse `SupportNavigationFragment` instead of `NavigationView`, whenever possible\n------------------------------------------------------------------------------\n\n`SupportNavigationFragment` is a wrapper for the convenience of handling the\n`NavigationView` lifecycle callbacks, so you don't need to manage these\ncallbacks themselves. This method is less error-prone and the preferred way to\nuse Navigation in your app. When using `SupportNavigationFragment`, be sure not\nto invoke `NavigationView` lifecycle events.\n\nIf using `NavigationView`, use strict ordering when invoking lifecycle methods\n------------------------------------------------------------------------------\n\n`NavigationView` hosts the Navigation map and closely follows the lifecycle\nevents as Android activities and fragments, taking specific actions when these\nlifecycle events are invoked. `NavigationView` executes multiple initializations\non `NavigationView#onCreate` and `NavigationView#onStart`, and cleanups on\n`NavigationView#onStop` and `NavigationView#onDestroy`, as well as when other\nlifecycle events are processed.\n\n`NavigationView` lifecycle methods have the same meaning as they do for Android\nactivities or fragments. For example, `onCreate()` of `NavigationView` roughly\ntranslates to and should be invoked by lifecycle callbacks from the Android\nactivity or fragment. Since the `NavigationView` lifecycle callbacks are based\non and invoked in the same order as the Android lifecycle callbacks, strong\nordering of these NavigationView methods is required. Otherwise, you may\nexperience [memory\nleaks](/maps/documentation/navigation/android-sdk/memory-best-practices), UI\nerrors, location not being updated, and other issues.\n\nFor more information about the Android activity lifecycle, see the\n[Activity-lifecycle concepts](https://developer.android.com/guide/components/activities/activity-lifecycle#alc)\nsection in the Android developer documentation.\n\nThe following table shows when other lifecycle methods should be invoked, after\nspecified lifecycle methods:\n\n| Lifecycle method | Invoked where in the activity lifecycle | Invoked after which lifecycle method |\n|----------------------------|-------------------------------------------------------------------------|--------------------------------------|\n| `onConfigurationChanged()` | Invoked when the UI is in the foreground and the configuration changes. | Always after `onStart()` |\n| `onTrimMemory()` | Invoked when an activity is in the background. | Always after `onPause()` |\n| `onSaveInstance()` | Invoked before an activity is destroyed. | Always after` onStop()` |\n\nDon't call these lifecycle methods multiple times without calling the\ncorresponding closing method first. In addition, keep in mind that if some of\nthese Android lifecycle callbacks are managed by the app itself, and the\n`NavigationView` is added to the fragment after creation or start, the app\nshould call the specific methods themselves in proper order in order to\ncorrectly initialize the Navigation SDK.\n\nFor additional guidance on using these methods, see the [Navigation SDK demo\napp](https://github.com/googlemaps-samples/android-navigation-samples/blob/4afba69715ebf6c176a2eb438f86d06c23065e7f/navigation-sample/app/src/main/java/com/example/navigationapidemo/NavViewActivity.kt#L279).\n\nIf using `NavigationView`, invoke lifecycle events from the activity or fragment, not both\n------------------------------------------------------------------------------------------\n\nTo keep the strict ordering of the lifecycle methods, invoke these events from\neither the activity or fragment lifecycle callbacks, which receive these events\nin order. This approach ensures that apps don't need to coordinate between\nfragments and activities and cause duplicate calls."]]