0
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-11-09

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

目標設定

一覧に戻る

課題

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

Github

Gradle等の変更

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

テスト実装

NavigationTestActivity.kt
class NavigationTestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_navigation_test)
    }
}
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>
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
    }
}
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>
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
    }
}
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>
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>
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>
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>
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>
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>

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0