오늘도 더 나은 코드를 작성하였습니까?

중첩 네비 그래프 (Nested Navi Graph) 본문

Android Jetpack Architecture/Navigation

중첩 네비 그래프 (Nested Navi Graph)

hik14 2021. 11. 19. 12:14

일련의 destination을 모은 navi graph는 root graph 라는 상위 탐색 그래프 내에 중첩 그래프로 그룹화될 수 있습니다.

중첩 그래프는 자체 포함된 로직 흐름과 같은 앱의 UI 섹션을 구성하고 재사용하는 데 유용합니다.

 

중첩 그래프는 destination을 캡슐화합니다. 루트 그래프와 마찬가지로 중첩 그래프에는 start destination 식별된 destination이 있어야 합니다. 루트 그래프의 대상과 같이 중첩 그래프 외부의 대상은 시작 destination을 통해서만 중첩 그래프에 액세스합니다.

 

중첩그래프 생성하기.

 

1. 탐색 편집기에서 Shift 키를 길게 누른 상태에서 중첩 그래프에 포함할 대상을 선택

 

2.  컨텍스트 메뉴를 마우스 오른쪽 버튼으로 클릭하여 열고 Move to Nested Graph > New Graph를 선택합니다.

destination 중첩 그래프에 포함되어 있습니다

 

3. 중첩 그래프를 클릭합니다. 다음 속성은 Attributes 패널에 표시됩니다.

  •  Type - 'Nested Graph'를 포함합니다.
  • ID - 중첩 그래프의 시스템 지정 ID를 포함합니다. 이 ID는 코드에서 중첩 그래프를 참조하는 데 사용됩니다.

4. 중첩 그래프를 더블클릭하여 destination을 표시합니다.

 

5. Text 탭을 클릭하여 XML 뷰를 전환합니다. 중첩된 탐색 그래프가 그래프에 추가되었습니다.

nested 탐색 그래프에는 자체 navigation 요소와 자체 ID 및 중첩 그래프의 첫 번째 대상을 가리키는 startDestination 속성이 있습니다.

 

<include>를 사용하여 다른 탐색 그래프 참조

탐색 그래프 내에서 include를 사용하여 다른 그래프를 참조할 수 있습니다

 

기능적으로는 중첩 그래프를 사용하는 것과 같지만 아래 예에서와 같이 include를 사용하면 다른 프로젝트 모듈 또는 라이브러리 프로젝트의 그래프를 사용할 수 있습니다.

 

Root Navi Graph

<!-- (root) nav_graph.xml -->
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/nav_graph"
    app:startDestination="@id/fragment">

    <include app:graph="@navigation/included_graph" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.myapplication.BlankFragment"
        android:label="Fragment in Root Graph"
        tools:layout="@layout/fragment_blank">
        <action
            android:id="@+id/action_fragment_to_second_graph"
            app:destination="@id/second_graph" />
    </fragment>

    ...
</navigation>

Second Navi Graph

<!-- included_graph.xml -->
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/second_graph"
    app:startDestination="@id/includedStart">

    <fragment
        android:id="@+id/includedStart"
        android:name="com.example.myapplication.IncludedStart"
        android:label="fragment_included_start"
        tools:layout="@layout/fragment_included_start" />
</navigation>

 

'Android Jetpack Architecture > Navigation' 카테고리의 다른 글

Destination으로 이동하기.  (0) 2021.11.19
Global Action  (0) 2021.11.19
Navigation graph design 네비 그래프 디자인  (0) 2021.11.18
destinations 생성  (0) 2021.11.15
Navigation component 시작하기  (0) 2021.11.12