7mood
7mood

Reputation: 45

Transfer from fragment to another in kotlin

I am trying to make a button that will go to another fragment, but instead, it shows the second fragment above the first one, I really need help with the fragments because am new.

Here is my code for the first fragment:

 class ToolsFragment : Fragment() {


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val root = inflater.inflate(R.layout.fragment_tools, container, false)
        val calcButton = root.findViewById<Button>(R.id.calc_button)

        calcButton.setOnClickListener {
            Log.d("ToolsFragment", "Calc Button Clicked")
            val fragment = MarksCalc()
            val fragmentManager = activity!!.supportFragmentManager
            val fragmentTransaction = fragmentManager.beginTransaction()
            fragmentTransaction.replace(R.id.nav_host_fragment, fragment)
            fragmentTransaction.addToBackStack(null)
            fragmentTransaction.commit()
        }

        return root
    }
}

Here is my second fragment

package com.example.itclubbeta

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button

class MarksCalc : Fragment() {

    override fun onCreateView(inflater: LayoutInflater,
                              container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_marks_calc, container, false)
    }
}

Here is nav_host_fragment

<?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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main_page">

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

Upvotes: 0

Views: 502

Answers (1)

edd
edd

Reputation: 32

you can actually add an action to your fragment in navigation graph

example:

<fragment
    android:id="@+id/fragment_id_here"
    android:name="fragment_name_here">
    <action
        android:id="@+id/your_main_fragment_id_to_your_next_fragment_id
        app:destination="@id/id_of_the_next_fragment_to_display"/>
</fragment>

and now you can call your action via findNavController

calcButton.setOnClickListener {
        Log.d("ToolsFragment", "Calc Button Clicked")
        findNavController().navigate(
            R.id.your_main_fragment_id_to_your_next_fragment_id
        )
    }

learn more about navigation here https://developer.android.com/guide/navigation/navigation-design-graph

Upvotes: 1

Related Questions