@@ -26,13 +26,14 @@ import androidx.appcompat.app.AppCompatActivity
2626import androidx.fragment.app.Fragment
2727import androidx.fragment.app.FragmentTransaction
2828import com.example.navigationapidemo.EdgeToEdgeUtil.EdgeToEdgeMarginConfig
29+ import com.example.navigationapidemo.layoutdelegate.LayoutDelegateApplier
2930import com.google.android.gms.maps.SupportMapFragment
3031import com.google.android.libraries.navigation.NavigationApi
3132import com.google.android.libraries.navigation.NavigationApi.NavigatorListener
33+ import com.google.android.libraries.navigation.NavigationFragment
3234import com.google.android.libraries.navigation.Navigator
3335import com.google.android.libraries.navigation.Navigator.RouteStatus
3436import com.google.android.libraries.navigation.SimulationOptions
35- import com.google.android.libraries.navigation.SupportNavigationFragment
3637import com.google.android.libraries.navigation.Waypoint
3738import com.google.android.libraries.navigation.Waypoint.UnsupportedPlaceIdException
3839import com.google.android.libraries.places.api.model.Place
@@ -46,17 +47,17 @@ import java.lang.ref.WeakReference
4647 * Customers of the v1 Navigation SDK historically have integrated the Google Play Services Maps
4748 * client into their builds side-by-side with the Navigation SDK. In that usage pattern, the
4849 * adopting app would show a SupportMapFragment while the driver is out-of-navigation, and switch
49- * over to a SupportNavigationFragment once the driver has selected a destination. At the end of the
50- * trip, or upon user cancellation, the customer would return to showing a SupportMapFragment.
50+ * over to a NavigationFragment once the driver has selected a destination. At the end of the trip,
51+ * or upon user cancellation, the customer would return to showing a SupportMapFragment.
5152 *
5253 * This activity mimics that behavior to support any customers who want to temporarily continue this
5354 * usage pattern. We recommend that v1 customers eventually transition to exclusively using a
54- * NavigationView or SupportNavigationFragment , since these classes support the GoogleMap interface.
55+ * NavigationView or NavigationFragment , since these classes support the GoogleMap interface.
5556 */
5657class SwappingMapAndNavActivity : AppCompatActivity () {
5758 private var navigator: Navigator ? = null
5859 private lateinit var mapFragment: SupportMapFragment
59- private lateinit var navigationFragment: SupportNavigationFragment
60+ private lateinit var navigationFragment: NavigationFragment
6061 private var arrivalListener: Navigator .ArrivalListener ? = null
6162
6263 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -70,8 +71,13 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
7071 listOf (EdgeToEdgeMarginConfig (view = findViewById(R .id.container)))
7172 )
7273
73- mapFragment = SupportMapFragment ()
74- navigationFragment = SupportNavigationFragment ()
74+ mapFragment =
75+ supportFragmentManager.findFragmentByTag(MAP_FRAGMENT_TAG ) as ? SupportMapFragment
76+ ? : SupportMapFragment .newInstance()
77+ navigationFragment =
78+ supportFragmentManager.findFragmentByTag(NAVIGATION_FRAGMENT_TAG ) as ? NavigationFragment
79+ ? : NavigationFragment .newInstance()
80+ LayoutDelegateApplier .applyLayoutDelegateFromIntent(intent, navigationFragment)
7581
7682 // Start with a basic map on the screen.
7783 if (savedInstanceState == null ) {
@@ -198,7 +204,7 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
198204 */
199205 fun stopTripAndShowMapFragment (unused : MenuItem ? ) {
200206 navigator?.clearDestinations()
201- if (supportFragmentManager.findFragmentById(R .id.container) is SupportNavigationFragment ) {
207+ if (supportFragmentManager.findFragmentById(R .id.container) is NavigationFragment ) {
202208 detachOldFragmentAndAddOrAttachNewFragment(
203209 /* fragmentToDetach=*/ navigationFragment,
204210 /* fragmentToAttach=*/ mapFragment,
@@ -208,19 +214,18 @@ class SwappingMapAndNavActivity : AppCompatActivity() {
208214 }
209215
210216 private fun registerArrivalListener () {
211- arrivalListener =
212- Navigator .ArrivalListener {
213- showToast(" User has arrived at the destination!" )
214- navigator?.stopGuidance()
217+ arrivalListener = Navigator .ArrivalListener {
218+ showToast(" User has arrived at the destination!" )
219+ navigator?.stopGuidance()
215220
216- // Stop simulating vehicle movement.
217- if (BuildConfig .DEBUG ) {
218- navigator?.simulator?.unsetUserLocation()
219- }
220-
221- // Switch back to the MapView.
222- stopTripAndShowMapFragment(/* unused= */ null )
221+ // Stop simulating vehicle movement.
222+ if (BuildConfig .DEBUG ) {
223+ navigator?.simulator?.unsetUserLocation()
223224 }
225+
226+ // Switch back to the MapView.
227+ stopTripAndShowMapFragment(/* unused= */ null )
228+ }
224229 navigator?.addArrivalListener(arrivalListener)
225230 }
226231
0 commit comments