a-2-3(4).Navigation画面遷移とデータ受け渡し

### a-2-3(4).Navigation画面遷移とデータ受け渡し

#### 目標設定
[一覧に戻る](https://qiita.com/ryouta33/items/57f5e51a26086151a0da)

#### 課題
3. Navigationを利用して画面遷移ができるか。
3. Navigationを利用して画面遷移後に元の画面に戻ることができるか。
3. Navigationを利用して画面遷移時にパラメーターを渡すことができるか。

#### Github

https://github.com/wakizaka24/AndroidTest

#### Gradle等の変更
NavigationとNavigationのパラメーター渡しのGoogle標準のライブラリを適用する。
差分の画像を添付する。

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/b2e7741e-985a-91a3-e5fb-ce9e6a9f2d8d.png
" width="1000" />

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/e6207fc1-8c38-a3a1-1439-da257a0703ac.png
" width="1000" />

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/80770eeb-b6af-4b11-5d5b-047f42b1c85b.png
" width="1000" />

#### テスト実装

```kotlin:NavigationTestActivity.kt
class NavigationTestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_test)
    }
}
```

```xml:activity_navigation_test.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/navigation_test"
        app:defaultNavHost="true" />

</androidx.constraintlayout.widget.ConstraintLayout>
```

```kotlin:NavigationTestScreen1Fragment.kt
class NavigationTestScreen1Fragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        super.onCreateView(inflater, container, savedInstanceState)

        val view = inflater.inflate(R.layout.fragment_navigation_test_screen1, container,
            false)

        view.findViewById<Button>(R.id.button1).setOnClickListener {
            // 3. Navigationを利用して画面遷移時にパラメーターを渡すことができるか。
            // ・可能でした。
            val action = NavigationTestScreen1FragmentDirections.next1("遷移元のパラメーター")
            // 1. Navigationを利用して画面遷移ができるか。
            // ・可能でした。
            findNavController().navigate(action)
        }

        val name = arguments?.getString("name")
        name?.run {
            val textView = view.findViewById<TextView>(R.id.textView1)
            textView.text = name
        }

        return view
    }
}
```

```xml:fragment_navigation_test_screen1.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Screen1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Fragment Button"
        android:textAllCaps="false"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        android:layout_marginTop="11dp"
        tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
```

```kotlin:NavigationTestScreen2Fragment.kt
class NavigationTestScreen2Fragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View {
        super.onCreateView(inflater, container, savedInstanceState)

        val view = inflater.inflate(R.layout.fragment_navigation_test_screen2, container,
            false)

        view.findViewById<Button>(R.id.button1).setOnClickListener {
            // 2. Navigationを利用して画面遷移後に元の画面に戻ることができるか。
            // ・可能でした。
            val action = NavigationTestScreen2FragmentDirections.next2("遷移先のパラメーター")
            findNavController().navigate(action)
        }

        val name = arguments?.getString("name")
        name?.run {
            val textView = view.findViewById<TextView>(R.id.textView1)
            textView.text = name
        }

        return view
    }
}
```

```xml:fragment_navigation_test_screen2.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Screen2"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="HardcodedText" />

    <Button
        android:id="@+id/button1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:text="Fragment Button"
        android:textAllCaps="false"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView1"
        android:layout_marginTop="11dp"
        tools:ignore="HardcodedText" />
</androidx.constraintlayout.widget.ConstraintLayout>
```

```xml:navigation_test.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/navigation_test"
    app:startDestination="@id/fragment1">

    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.androidtest.NavigationTestScreen1Fragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_navigation_test_screen1" >
        <action
            android:id="@+id/next1"
            app:enterAnim="@anim/from_right"
            app:exitAnim="@anim/to_left"
            app:destination="@id/fragment2">
            <argument
                android:name="name"
                app:argType="string" />
        </action>
    </fragment>

    <fragment
        android:id="@+id/fragment2"
        android:name="com.example.androidtest.NavigationTestScreen2Fragment"
        android:label="fragment_first"
        tools:layout="@layout/fragment_navigation_test_screen2" >
        <action
            android:id="@+id/next2"
            app:enterAnim="@anim/from_left"
            app:exitAnim="@anim/to_right"
            app:destination="@id/fragment1">
            <argument
                android:name="name"
                app:argType="string" />
        </action>
    </fragment>
</navigation>
```

```xml:from_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>
```

```xml:from_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="0.0" android:toAlpha="1.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>
```

```xml:to_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>
```

```xml:to_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="@android:integer/config_mediumAnimTime" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.0"
        android:duration="@android:integer/config_mediumAnimTime" />
</set>
```

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/0b1d73d8-2b41-31b7-6891-81c65d68c7e8.png
" width="300" />

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/21ae4767-1baa-2d52-7fbc-cb5f4a4d22f5.png
" width="300" />

<img src="https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/1545849/981e4873-d24c-c416-0b54-2bfe2d2a46ff.png
" width="300" />