user12586023
user12586023

Reputation:

Passing data between data fragments

Hi I have made an app and trying to pass data between two fragments in an app

nav_graph.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/nav_graph"
app:startDestination="@id/title2">
<fragment
    android:id="@+id/title2"
    android:name="com.example.order10.title"
    android:label="title" >
    <action
        android:id="@+id/action_title2_to_pageone"
        app:destination="@id/pageone" />
   </fragment>
   <fragment
    android:id="@+id/pageone"
    android:name="com.example.order10.pageone"
    android:label="pageone" >
    <action
        android:id="@+id/action_pageone_to_finalpage"
        app:destination="@id/finalpage" >
        <argument android:defaultValue="Hi"
            android:name="text" />
    </action>
    </fragment>
    <fragment
    android:id="@+id/finalpage"
    android:name="com.example.order10.finalpage"
    android:label="fragment_finalpage"
    tools:layout="@layout/fragment_finalpage" >
    <argument
        android:name="order_text"
        app:argType="string"
        android:defaultValue="Hi" />
  </fragment>
   <fragment
    android:id="@+id/about"
    android:name="com.example.order10.about"
    android:label="Order 1.0" />
 </navigation>

Code for passing data

fun navigation(text:String){
    var args= bundleOf()
    args.putString("order_text",text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)


}      navigation("This is order one ")

Code for receiving data

 val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=arguments?.getString("order_text")

But the it doesn't work it doesn't even show the default text, I have applied all plugins and dependencies

Upvotes: 1

Views: 251

Answers (2)

milad salimi
milad salimi

Reputation: 1660

Code for passing data change to this :

fun navigation(text:String){

    val args = bundleOf("order_text" to text)
    view?.findNavController()?.navigate(R.id.action_pageone_to_finalpage,args)
}

Code for receiving data change to this :

val receiveData = arguments?.getString("order_text")!!

val order_text:TextView?=view?.findViewById(R.id.order_text)
    order_text?.text=receiveData

Update :

Change your finalPage to this :

class finalpage : Fragment() {


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

        return inflater.inflate(R.layout.fragment_finalpage, container, false)
    }

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

        val receiveData = arguments?.getString("order_text")!!
        val order_text:TextView?=view.findViewById(R.id.order_text)
        order_text?.text=receiveData
    }
}

YOU CAN USE COMPONENTS AFTER THAT YOU DEFINE YOUR VIEW

Upvotes: 0

Kulbuto
Kulbuto

Reputation: 79

For passing data :

val direction = pageoneDirections.actionPageOneToFinalPage("your text")
view?.findNavController()?.navigate(direction)

For receiving data :

In your class :

val args: finalpageArgs by navArgs()

In your onCreateView :

val order_text = args.order_text

Upvotes: 1

Related Questions