Search code examples
androidandroid-architecture-componentsandroid-navigationandroid-jetpackandroid-architecture-navigation

why my fragment doesn't show in the navigation controller?


I'm new in using navigation controller for Android.

Here is the xml of my main activity:

<androidx.constraintlayout.widget.ConstraintLayout
        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"
        tools:context=".Activities.MainActivity">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/main_activity_toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:elevation="4dp" app:layout_constraintHorizontal_bias="0.0">
    </androidx.appcompat.widget.Toolbar>

    <fragment
            android:id="@+id/nav_main_host_fragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/main_activity_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/main_graph"
            app:defaultNavHost="true"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

my kotlin Main Activity:

class MainActivity : AppCompatActivity(),NavController.OnDestinationChangedListener {
    lateinit var navController : NavController

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

        navController = Navigation.findNavController(this,R.id.nav_main_host_fragment)
        navController.addOnDestinationChangedListener(this)

        setupActionBar()
    }

    override fun onDestinationChanged(
        controller: NavController,
        destination: NavDestination,
        arguments: Bundle?) {
    }

    private fun setupActionBar() {
        setSupportActionBar(main_activity_toolbar)
        supportActionBar?.title = null

        val appBarConfiguration = AppBarConfiguration(
            setOf(
                // set some destination as the top hierarchy destination
                // to make the up button doesn't show.
                R.id.destination_latestMessage
            ))

        NavigationUI.setupWithNavController(
            main_activity_toolbar,
            navController,
            appBarConfiguration)
    }
}

my fragment 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"
    tools:context=".Fragments.LatestMessageFragment"
    android:id="@+id/ConstraintLayout_latestMessage">

    <!-- TODO: Update blank fragment layout -->
    <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="latest message"
            android:id="@+id/textView" 
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button" 
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" 
            app:layout_constraintBottom_toBottomOf="@+id/textView" 
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/textView"
            android:layout_marginStart="8dp" 
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

my fragment kotlin file:

class LatestMessageFragment : Fragment() {

    lateinit var mContext : Context
    lateinit var mActivity : FragmentActivity
    lateinit var fragmentView : View

    override fun onAttach(context: Context) {
        super.onAttach(context)
        mContext = context
        activity?.let { mActivity = it }

    }

    override fun onCreateView(
        inflater: LayoutInflater, 
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        fragmentView = inflater.inflate(
            R.layout.fragment_latest_message, 
            container, 
            false)
        setHasOptionsMenu(true) // to setup custom menu on the action bar
        return fragmentView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.log_out_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        return when (id) {
            R.id.logout-> {
                true
            }
            else -> super.onOptionsItemSelected(item)
        }
    }
}

and here is the graph xml file:

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

    <fragment android:id="@+id/destination_latestMessage" 
          android:name="com.lakuin.cs.Fragments.LatestMessageFragment"
          android:label="Latest Message"
          tools:layout="@layout/fragment_latest_message"/>
</navigation>

as you can see, on my destination fragment, it has button and text view, but when I run the app, the text view and the button don't appear on the screen.

I only have one fragment, but that fragment doesn't show on the screen, but the toolbar and its menu I set appears on the screen

I use the depedency below:

//Navigation controller
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-rc02'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-rc02'

what went wrong in here ?


Solution

  • It's a layout issue of your nav fragment as @Ranjan point out for you. It's not related to Navigation UI. Use:

    app:layout_constraintBottom_toBottomOf="parent"
    

    Instead of:

    app:layout_constraintBottom_toTopOf="parent"
    

    In your NavControllerFragmnet layout.