简体   繁体   中英

How to pass data to a fragment from activity when I fill a searchview?

I have a searchview in activity and there is a fragment in it. When I try to transfer data to a fragment when searching, they do not reach it. How can I do that? In this method

override fun onQueryTextChange(newText: String?): Boolean {
        return false
    }

Add this to your fragment:

    companion object {
    fun newInstance(data: String) = YourFragment().apply {
        this.data= data
    }

}

And call the fragment like shown below:

  supportFragmentManager.beginTransaction().add(R.id.mainFragment,YourFragment.newInstance(yourData)).commit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM