Home | History | Annotate | Download | only in navigation
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package androidx.navigation;
     18 
     19 import android.content.Context;
     20 import android.os.Bundle;
     21 import android.support.annotation.NonNull;
     22 import android.support.annotation.Nullable;
     23 
     24 /**
     25  * A Navigator built specifically for {@link NavGraph} elements. Handles navigating to the
     26  * correct destination when the NavGraph is the target of navigation actions.
     27  */
     28 @Navigator.Name("navigation")
     29 public class NavGraphNavigator extends Navigator<NavGraph> {
     30     private Context mContext;
     31 
     32     /**
     33      * Construct a Navigator capable of routing incoming navigation requests to the proper
     34      * destination within a {@link NavGraph}.
     35      * @param context
     36      */
     37     public NavGraphNavigator(@NonNull Context context) {
     38         mContext = context;
     39     }
     40 
     41     /**
     42      * Creates a new {@link NavGraph} associated with this navigator.
     43      * @return
     44      */
     45     @NonNull
     46     @Override
     47     public NavGraph createDestination() {
     48         return new NavGraph(this);
     49     }
     50 
     51     @Override
     52     public void navigate(@NonNull NavGraph destination, @Nullable Bundle args,
     53             @Nullable NavOptions navOptions) {
     54         int startId = destination.getStartDestination();
     55         if (startId == 0) {
     56             throw new IllegalStateException("no start destination defined via"
     57                     + " app:startDestination for "
     58                     + (destination.getId() != 0
     59                             ? NavDestination.getDisplayName(mContext, destination.getId())
     60                             : "the root navigation"));
     61         }
     62         NavDestination startDestination = destination.findNode(startId, false);
     63         if (startDestination == null) {
     64             final String dest = NavDestination.getDisplayName(mContext, startId);
     65             throw new IllegalArgumentException("navigation destination " + dest
     66                     + " is not a direct child of this NavGraph");
     67         }
     68         dispatchOnNavigatorNavigated(destination.getId(), BACK_STACK_DESTINATION_ADDED);
     69         startDestination.navigate(args, navOptions);
     70     }
     71 
     72     @Override
     73     public boolean popBackStack() {
     74         return false;
     75     }
     76 }
     77