I want to passing data with intent extra from fragment to activity, Usually I use anko common intent from activity to activity
startactivity<secondActivity>("values" to "123")
but in my case, I want to passing data from fragment to activity like this
activity?.startactivity<secondActivity>("values" to "123")
when I want to get String Extra in my activity,
val values: String = ""
val intent = intent
values = intent.getStringExtra("values")
I get Error
intent.getstringextra must not be null
Do you have a solution for my case? because I do that to get extra from activity to activity no problem
Use kotlin Null Safety. if its null it wont assign value
var values: String = ""
val intent = intent
intent.getStringExtra("values")?.let {
values=it
}
The problem is that you've declared values
variable as not nullable i.e. the compiler will:
null
or possibly null
valuesThe Intent.getStringExtra
may return null
and thus the compiler is complaining.
You can declare the values
variable as possibly null
and handle that case in code e.g.:
val values: String? = intent.getStringExtra("values")
if(values == null){
finish()
return;
}
Or assign a default value in case the intent does not have values
extra:
val values = intent.getStringExtra("values") ?: "Default values if not provided"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With