-1

navigation_main file

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

    <fragment
        android:id="@+id/navigation_main_countries_fragment"
        android:name="com.test.app.fragments.CountriesFragment"
        tools:layout="@layout/fragment_countries" />

    <fragment
        android:id="@+id/navigation_main_categories_fragment"
        android:name="com.test.app.fragments.CategoriesFragment"
        tools:layout="@layout/fragment_categories" />

</navigation>

Is it possible to remove app:startDestination="@id/navigation_main_countries_fragment" from navigation_main file and do it programmatically? If the int x was 0 then run countries_fragment otherwise categories_fragment.

I'm using Java language.

Please test your answer before posting it because many people posted their answers but unfortunately the answers do not work.

Phil Dukhov
  • 67,741
  • 15
  • 184
  • 220
Taha Sami
  • 1,565
  • 1
  • 16
  • 43
  • This question totally duplicates your [previous one](https://stackoverflow.com/questions/71062281/how-can-i-add-startdestination-programmatically-accoridng-the-value). The only difference I see is that you actually asking for Java solutions. You should've edit that question and add this detail, instead of creating a new one. – Phil Dukhov Feb 10 '22 at 13:54
  • Also why you keep adding [android-jetpack-compose] tag? [Compose](https://developer.android.com/jetpack/compose) is a completely different kotlin-only technology, it doesn't seems like your question has anything to do with this tag. – Phil Dukhov Feb 10 '22 at 13:56

2 Answers2

1

The navigation documentation explains it pretty well on Conditional Navigation using navigation components.

Michael Ndiritu
  • 292
  • 2
  • 4
  • But the question still is it possible to delay startDestination or not, Because if it is not supported then I'll use your solution. I still trying since the morning – Taha Sami Feb 10 '22 at 13:42
1

Is it possible to remove app:startDestination="@id/navigation_main_countries_fragment" from navigation_main file

No.

and do it programmatically?

Yes. but you should override it.

You must define a dummy destination in the xmL and then change it in your code:

<?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/main_graph"
    app:startDestination="@id/empty_fragment">

    <fragment
        android:id="@+id/empty_fragment"
        android:name="com.example.EmptyFragment"
        android:label="EmptyFragment" />
</navigation>
val targetDestination = if (x == 0)
    R.id.navigation_main_countries_fragment
else
    R.id.navigation_main_categories_fragment

val navController = ....
navController.graph = navController.graph.apply { startDestination = targetDestination }
beigirad
  • 4,986
  • 2
  • 29
  • 52