Dimple patel
Dimple patel

Reputation: 152

Kotlin with Fragments

    var bundle : Bundle ? =null

    bundle?.putString("text",text)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        frag = Fragment.instantiate(context,Fragment2::class.java.name) as Fragment2
    }

    frag?.arguments=bundle

    fragmentManager.beginTransaction().replace(R.id.contentPanel1,frag).commit()

I Have written these code on fragment1 and passing the data to it

On Fragment 2 I am receving the bundle as null can anyone solve it

var bundle : Bundle ?
bundle = arguments

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    Toast.makeText(context,bundle.toString(),Toast.LENGTH_LONG).show()
}

Upvotes: 1

Views: 753

Answers (2)

Vishal Sojitra
Vishal Sojitra

Reputation: 496

Please follow below code

 /**
 * Use this factory method to create a new instance of
 * this fragment using the provided parameters.
 *
 * @return A new instance of fragment ProfileFragment.
 */
// TODO: Rename and change types and number of parameters
@JvmStatic
fun newInstance(bundle: Bundle) = ProfileFragment().apply { arguments 
= bundle }
}

startFragment(ProfileFragment.newInstance(Bundle()))

Upvotes: 0

KuLdip PaTel
KuLdip PaTel

Reputation: 1069

Issue is that you don't initialize var of bundle.look below.

val bundle = Bundle()

bundle.putString("text",text)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    frag = Fragment.instantiate(context,Fragment2::class.java.name) as Fragment2
}

frag?.arguments=bundle

fragmentManager.beginTransaction().replace(R.id.contentPanel1,frag).commit()

Upvotes: 5

Related Questions