Thursday, 21 November 2019

Why is navigation destination lost when I launch a action for the second time with navigation in Android Studio?

I use navigation in my project.

The Image 1 will be displayed when the app start, I click Next button in Image 1, the Image 2 will be displayed, then I click Back button in Image 2, the image 1 will be displayed again.

Now I click Next button in Image 1 again, the app crash, I get the error 'navigation destination com.example.myapplication:id/actionTwo is unknown to this NavController', why?

Why can't NavHostFragment.findNavController(this).navigate(OneFrDirections.actionTwo()) be launched for the second time ?

You can download the test code at https://www.dropbox.com/s/1ad47eqi4iwig5v/MyNAV.zip?dl=0

my.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/my"
    app:startDestination="@+id/One">

    <fragment
        android:id="@+id/One"
        android:name="com.example.myapplication.OneFr"
        android:label="One">

        <action
            android:id="@+id/actionTwo"
            app:destination="@id/Two" />

    </fragment>

    <fragment
        android:id="@+id/Two"
        android:name="com.example.myapplication.TwoFr"
        android:label="Two">

        <action
            android:id="@+id/actionOne"
            app:destination="@id/One" />
    </fragment>

</navigation>

OneFr.kt

class OneFr : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?): View? =
        inflater.inflate(R.layout.one, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

         view.findViewById<Button>(R.id.btnNext).setOnClickListener {
            NavHostFragment.findNavController(this).navigate(OneFrDirections.actionTwo())
        }

    }

}

TwoFr.kt

class TwoFr : Fragment() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?): View? =
        inflater.inflate(R.layout.two, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // Handle back button press
        view.findViewById<Button>(R.id.btnBack).setOnClickListener {
            fragmentManager?.popBackStack()
        }

    }

}

one.xml

<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">

    <Button
        android:id="@+id/btnNext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

two.xml

<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">

    <Button
        android:id="@+id/btnBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:keepScreenOn="true"
    tools:context=".MainActivity">

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

</FrameLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

Image 1

enter image description here

Image 2

enter image description here



from Why is navigation destination lost when I launch a action for the second time with navigation in Android Studio?

No comments:

Post a Comment