11package com .d4rk .androidtutorials .java .ui .screens .main ;
22
33import android .Manifest ;
4+ import android .animation .ValueAnimator ;
45import android .annotation .SuppressLint ;
56import android .app .Activity ;
67import android .content .Intent ;
@@ -253,20 +254,6 @@ private void observeViewModel() {
253254 }
254255 }
255256
256- NavOptions forwardOptions = new NavOptions .Builder ()
257- .setEnterAnim (R .anim .fragment_spring_enter )
258- .setExitAnim (R .anim .fragment_spring_exit )
259- .setPopEnterAnim (R .anim .fragment_spring_pop_enter )
260- .setPopExitAnim (R .anim .fragment_spring_pop_exit )
261- .build ();
262-
263- NavOptions backwardOptions = new NavOptions .Builder ()
264- .setEnterAnim (R .anim .fragment_spring_pop_enter )
265- .setExitAnim (R .anim .fragment_spring_pop_exit )
266- .setPopEnterAnim (R .anim .fragment_spring_enter )
267- .setPopExitAnim (R .anim .fragment_spring_exit )
268- .build ();
269-
270257 androidx .navigation .NavDestination destination = navController .getCurrentDestination ();
271258 if (destination != null ) {
272259 currentNavIndex = navOrder .get (destination .getId (), currentNavIndex );
@@ -281,7 +268,7 @@ private void observeViewModel() {
281268 return true ;
282269 }
283270 int newIndex = navOrder .get (item .getItemId ());
284- NavOptions options = newIndex > currentNavIndex ? forwardOptions : backwardOptions ;
271+ NavOptions options = applyTopLevelNavAnimations ( new NavOptions . Builder ()). build () ;
285272 navController .navigate (item .getItemId (), null , options );
286273 currentNavIndex = newIndex ;
287274 return true ;
@@ -295,7 +282,7 @@ private void observeViewModel() {
295282 return true ;
296283 }
297284 int newIndex = navOrder .get (item .getItemId ());
298- NavOptions options = newIndex > currentNavIndex ? forwardOptions : backwardOptions ;
285+ NavOptions options = applyTopLevelNavAnimations ( new NavOptions . Builder ()). build () ;
299286 navController .navigate (item .getItemId (), null , options );
300287 currentNavIndex = newIndex ;
301288 return true ;
@@ -316,6 +303,21 @@ private void observeViewModel() {
316303 });
317304 }
318305
306+ private NavOptions .Builder applyTopLevelNavAnimations (NavOptions .Builder builder ) {
307+ if (ValueAnimator .areAnimatorsEnabled ()) {
308+ builder .setEnterAnim (R .anim .fragment_top_level_enter )
309+ .setExitAnim (R .anim .fragment_top_level_exit )
310+ .setPopEnterAnim (R .anim .fragment_top_level_enter )
311+ .setPopExitAnim (R .anim .fragment_top_level_exit );
312+ } else {
313+ builder .setEnterAnim (0 )
314+ .setExitAnim (0 )
315+ .setPopEnterAnim (0 )
316+ .setPopExitAnim (0 );
317+ }
318+ return builder ;
319+ }
320+
319321
320322 private void navigateToPreferredDestination (int preferredDestination ) {
321323 if (navController == null ) {
@@ -328,10 +330,11 @@ private void navigateToPreferredDestination(int preferredDestination) {
328330 lastPreferredStartDestination = preferredDestination ;
329331 return ;
330332 }
331- NavOptions options = new NavOptions .Builder ()
332- .setPopUpTo (graph .getStartDestinationId (), true )
333- .setLaunchSingleTop (true )
334- .build ();
333+ NavOptions options = applyTopLevelNavAnimations (
334+ new NavOptions .Builder ()
335+ .setPopUpTo (graph .getStartDestinationId (), true )
336+ .setLaunchSingleTop (true )
337+ ).build ();
335338 navController .navigate (preferredDestination , null , options );
336339 lastPreferredStartDestination = preferredDestination ;
337340 }
0 commit comments