Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Send data from one Fragment to another using Kotlin?
This example demonstrates how to send data from one Fragment to another using Kotlin.
Step 1 − Create a new project in Android Studio, go to File ? New Project and fill all required details to create a new project.
Step 2 − Add the following code to res/layout/activity_main.xml.
Step 3 − Create two FragmentActivity and add the codes which are given below.
fragmentOne.xml −
FirstFragment.kt −
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import kotlinx.android.synthetic.main.fragment_one.view.*
class FirstFragment : Fragment() {
private lateinit var communicator: Communicator
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_one, container, false) as ViewGroup
communicator = activity as Communicator
rootView.btnSend.setOnClickListener {
communicator.passData(rootView.editText.text.toString())
}
return rootView
}
}
fragmentTwo.xml −
FragmentTwo.kt −
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_two.view.*
class FragmentTwo : Fragment() {
var inputText: String? = ""
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_two, container, false)
inputText = arguments?.getString("inputText")
rootView.outPutTextView.text = inputText
return rootView
}
}
Step 4 − Add the following code to src/MainActivity.kt
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.FragmentTransaction
class MainActivity : AppCompatActivity(), Communicator {
public override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val firstFragment = FirstFragment()
supportFragmentManager.beginTransaction().replace(R.id.relativeLayout, firstFragment)
.commit()
}
override fun passData(editTextInput: String) {
val bundle = Bundle()
bundle.putString("inputText", editTextInput)
val transaction = this.supportFragmentManager.beginTransaction()
val fragmentTwo = FragmentTwo()
fragmentTwo.arguments = bundle
transaction.replace(R.id.relativeLayout, fragmentTwo)
transaction.addToBackStack(null)
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
transaction.commit()
}
}
Step 5 − Add the following code to androidManifest.xml
Let's try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project's activity files and click the Run
icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen −


Click here to download the project code.
