Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
interface Communicator {
fun passDataCom(editext_input: String)
}
class Fragment1: Fragment() {
lateinit var comm: Communicator
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_1, container, false)
comm = activity as Communicator
rootView.enter_btn.setOnClickListener {
comm.passDataCom(rootView.input_edittext.text.toString())
}
return rootView
}
}
class Fragment2: Fragment() {
var inputText: String? = ""
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_2, container, false)
inputText = arguments?.getString("input_txt")
rootView.output_textview.text = inputText
return rootView
}
}
class MainActivity : AppCompatActivity(), Communicator {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Set replace Main Activity content with the Fragment1 content
val Fragment1 = Fragment1()
supportFragmentManager.beginTransaction().replace(R.id.content_id, Fragment1).commit()
}
override fun passDataCom(editext_input: String) {
val bundle = Bundle()
bundle.putString("input_txt",editext_input)
val transaction = this.supportFragmentManager.beginTransaction()
val frag2 = Fragment2()
frag2.arguments = bundle
transaction.replace(R.id.content_id, frag2)
transaction.addToBackStack(null)
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
transaction.commit()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.