1

I'm relatively new to android Studio so i was just asking how can one send a variable lets say;

val Name = editName.text.toString() from one Fragment to another so you can use it in the Second Fragment?? I don't have the project its just a question i had.

adamadam
  • 37
  • 5

3 Answers3

5

Firstly i suggest you learn what is fragment if you do not know,and you can add some arguments in passing fragments this is the navigation link. https://developer.android.com/guide/navigation and i will explain a bit first do implementations of navigation from this link https://developer.android.com/guide/navigation/navigation-getting-started

then,create a navigation (documentation will help you) finally you will create arrows between fragments in navigation and put extras into you navigation as arguments without any code it is very easy argument documentation is here: https://developer.android.com/guide/navigation/navigation-pass-data

val bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)

and finally you can pass your data with these codes

Barney Stinson
  • 212
  • 3
  • 12
2

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.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/relativeLayout"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="8dp">
</RelativeLayout>

Step 3 − Create two FragmentActivity and add the codes which are given below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical"
   android:padding="8dp">
   <EditText
      android:id="@+id/editText"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="50dp"
      android:hint="Enter your message"
      android:textColorHint="@android:color/background_dark"
      android:textSize="16sp"
      android:textStyle="bold" />
   <Button
      android:id="@+id/btnSend"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:background="@android:color/holo_blue_dark"
      android:padding="10dp"
      android:text="Send Message"
      android:textColor="@android:color/white"
      android:textStyle="bold" />
</LinearLayout>

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 −

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:gravity="center"
   android:orientation="vertical">
   <TextView
      android:id="@+id/outPutTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:textColor="@android:color/background_dark"
      android:textSize="24sp"
      android:textStyle="bold" />
</LinearLayout>

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 5 − Add the following code to androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="app.com.kotlipapp">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" /&g;
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter&g;
      </activity>
   </application>
</manifest>

I took example following tutorial

Edgar
  • 860
  • 1
  • 17
  • 38
0

Do you want to do so while both Fragment are active on the same activity? or when a new fragment is replacing the previews one? Both are possible but require a different solution.

If both fragment are active at the same time you can create an interface and have all your fragment implement it. Here is an example with events sent from one fragment to the others:

abstract class ActionableFragment(@LayoutRes contentLayoutId: Int) : Fragment() {
        init { Fragment(contentLayoutId) }
        abstract fun processEvent(event: MyEventObject)

    fun sendEvent(event: MyEventObject) {
        val fragmentList = parentFragmentManager.fragments as MutableList<ActionableFragment>
        for (fragment in fragmentList) {
        fragment.processEvent(event)
    }
}

Code in the fragment:

   class FirstFragment : ActionableFragment(R.layout.fragment_one) {
    
        override fun processEvent(event: MyEventObject) {
            Util.log("FirstFragment", "${event.name}")
        }
    
        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
            // Inflate the layout for this fragment
            sendEvent(MyEventObject("This is an event with an Int",1))
            return inflater.inflate(R.layout.fragment_one, container, false)
        }
    }
Slamit
  • 465
  • 1
  • 6
  • 21