[Android] FragmentContainerView Navigation graph action 옵션 popUpTo, popUpToInclusive 알아보기 (1)
FragmentContainerView 학습중에 navGraph 속성으로 쉽게 프래그먼트 이동을 쉽게 설정할수 있다.
navGraph 속성으로 넣어주는 xml 구성에 대해 학습한 내용을 기록해 본다.

FragmentContainerView 사용하기 위한 과정은?
nav_graph_main.xml 파일 생성후 설정해보자

nav_graph_main.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"
android:id="@+id/nav_graph_main"
app:startDestination="@id/main_fragment">
<fragment
android:id="@+id/main_fragment"
android:name="com.minikode.fragment_viewmodel_demo.ui.main.fragment.MainFragment"
android:label="main">
<action
android:id="@+id/main_to_second"
app:destination="@id/second_fragment" />
</fragment>
<fragment
android:id="@+id/second_fragment"
android:name="com.minikode.fragment_viewmodel_demo.ui.main.fragment.SecondFragment"
android:label="second">
<action
android:id="@+id/second_to_main"
app:destination="@id/main_fragment" />
<action
android:id="@+id/second_to_third"
app:destination="@id/third_fragment" />
</fragment>
<fragment
android:id="@+id/third_fragment"
android:name="com.minikode.fragment_viewmodel_demo.ui.main.fragment.ThirdFragment"
android:label="third">
<action
android:id="@+id/third_to_second"
app:destination="@id/second_fragment" />
<action
android:id="@+id/third_to_fourth"
app:destination="@id/fourth_fragment" />
</fragment>
<fragment
android:id="@+id/fourth_fragment"
android:name="com.minikode.fragment_viewmodel_demo.ui.main.fragment.FourthFragment"
android:label="fourth">
<action
android:id="@+id/fourth_to_third"
app:destination="@id/third_fragment" />
</fragment>
</navigation>
총 4개 프래그먼트 생성한후
main <-> second <-> third <-> fourth 이렇게 이동하는 설정을 넣어서 확인해봤다.
여기서 <action> 태그 속성중 popUpTo 와 popUpToInclusive 의 값이 잘 이해가 되지 않아 실제로 값을 변경하면서 확인해봤다.
필자는 id=fourth_to_third에 넣고 확인해봤다.
<action
android:id="@+id/fourth_to_third"
app:popUpTo="@id/second_fragment"
app:popUpToInclusive="false"/>
주의 : 여기서 app:destination="@id/third_fragment" 속성을 제거하는 편이 이해하기 쉬울것이다.
왜냐하면 popUpTo 속성 값 프래그먼트 까지 백스택에서 pop 을 해서 그 페이지로 이동한다라는 의미하기 때문에 제거 하고 진행했다.
이것저것 값을 넣고 확인해본 바로
<action> 옵션중 app:popUpTo 의미
→ ex) app:popUpTo=”second_fragment” 라면 second_fragment 이후부터 스택은 pop 시키고 이동해라 라는 의미
<action> 옵션중 app:popUpInclusive 의미
→ ex) popUpTo 에 넣은 프래그먼트를 포함 시킬지 말지 여부
위처럼 popUpTo에 second_fragment 를 넣고, popUpToInclusive에 false 라면
action 부분이 이동할때
third_fragment 부터 현재페이지 백스택을 빼고 이동한다
라는 의미가 된다.
더 자세히 보면 main -> second -> third -> fourth 순서로 이동할때
@id/fourth_to_third action 으로 이동하는 순간에 third 백스택에서 pop 시키고 second 으로 간다는 의미이다.
그렇게 되면 fourth 에서 back버튼으로 누르게 되면 second 프래그먼트로 바로 이동한다.
여기서 popUpToInclusive 값이 true 라면?
second_fragment 를 포함하면서 백스택에서 pop 시키고 main 프래그먼트 간다는 의미
문서를 봐도 잘 이해되지 않는 부분이었는데 직접 확인해보니 이해가 되는 부분이었다.