Hi I am creating a SideActivity to gather some results and pass them back to Main Activity. However, the two strings value from the putextra are NULL rather than the string "20" and the jamSize "medium". Is there a way of passing the data properly?
Here in my Main Activity I have a setOnClickListener and a onActivityResult function.
jamButton.setOnClickListener {
var intent = Intent(this#MainActivity, SideActivity::class.java)
intent.putExtra("jamName", "raspberry")
intent.putExtra("jamPrice", "12.00")
startActivityForResult(intent, 1) // passing request code value 1
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == RESULT_OK) {
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
val jamSize:String = intent.getStringExtra("jamSize").toString()
val newJam = DataModel("Jam", "$jamSize", "$jamPrice")
list.add(0, newJam)
jamAdapter.notifyItemInserted(0)
}
}
Here is my Second Activity
completeBtn.setOnClickListener {
val jamPrice: String = textView2.text.toString()
val jamSize: String = textView3.text.toString()
val intent = Intent(this#SideActivity, MainActivity::class.java)
intent.putExtra("jamPrice", "20.00")
intent.putExtra("jamSize", jamSize)
setResult(Activity.RESULT_OK, intent)
finish()
}
In your onActivityResult don't use this :
val jamPrice:String = intent.getStringExtra("jamPrice").toString()
because the intent variable is the Intent of the activity instead use :
val jamPrice: String = data?.getStringExtra("jamPrice").toString()
Related
Halllo, my BarcodeActivity is called by startActivityForResult through my PCActivity. The value of the scanned barcode should then be returned back to the PCActivity and inserted in a text field there. Unfortunately, I do not get a value back. However, the app does not crash either. Here is my code.
PCActivity:
sn_mb.setDrawableRightTouch {
val intent = Intent(this#PCActivity, BarcodeActivity::class.java)
startActivityForResult(intent, 1)
}
[...]
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK && data != null) {
val returnedSN = intent.getStringExtra("return_sn")
sn_mb.setText(returnedSN)
} else {
sn_mb.setText("FEHLER!")
}
}
BarcodeActivity
saveBtn.setOnClickListener {
val sn = editTextBarcode.text.toString()
sn.toString()
if (sn!= "") {
val returnIntent:Intent = Intent()
returnIntent.putExtra("return_sn", sn)
setResult(Activity.RESULT_OK, returnIntent)
finish()
} else {
Toast.makeText(
applicationContext,
"Das ist keine gültige Seriennummer",
Toast.LENGTH_SHORT
).show()
}
}
I hope someone can explain or help me with this problem. Thank you very much.
You are extracting your data from the wrong place.
Replace
val returnedSN = intent.getStringExtra("return_sn")
with
val returnedSN = data.getStringExtra("return_sn")
The problem is you are not passing the scanned bar code value (sn) to the intent(returnIntent) in your BarCodeActivity.
First make sure sn is String since you want to pass a StringExtra, therefore:
val sn = editTextBarcode.text.toString()
And then pass sn to your return intent:
returnIntent.putExtra("return_sn", sn)
Notice that in your code you are passing integer 1 instead of sn.
EDIT:
One more minor fix to your code, didn't notice it:
val returnIntent:Intent = getIntent()
Notice that it's getIntent() instead of Intent()
I am trying to get data from one activity in other. But data always null.
val res = Intent()
res.putExtra("uri", imageUri)
setResult(Activity.RESULT_OK, res)
finish()
In the second activity I always get data as null. What is the problem? (imageUri is not null when I put it).
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if(resultCode == Activity.RESULT_OK){
//data == null, WHY???
}
}
I think you need to define the intent with the current context and target class.
val intent = Intent(this#HomeActivity,ProfileActivity::class.java) // define you activity in which you want to go
intent.putExtra("Username","John Doe")
startActivity(intent)
I'm trying to get the result of intent between 2 activities but something is wrong because I always get a resultCode = 0 in initial activity:
Code inside CarsFragment.kt
private fun startAddCarActivity() {
val intent = Intent(context, AddCarActivity::class.java)
startActivityForResult(intent, 1)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// ALWAYS GET requestCode = 1, resultCode = 0 and data = null !!
}
AddCarActivity.kt:
private fun startCarsNavigationActivity() {
intent.putExtra("car", car)
setResult(1, intent)
finish()
}
Problem:
I always get requestCode = 1, resultCode = 0 and data = null in CarsFragment.kt
Where is the problem ?
Result code of 0 means RESULT_CANCELED. This can happen if the Activity you are launching is launched into a different task, or if the user presses the BACK key, or if the launched Activity decides to return RESULT_CANCELED.
I need to pass multiple variables between more than two activities.
I know how to pass between one to another, but not from the first to the third.
Activity One
btnNext.setOnClickListener {
val i = Intent(this, Activity2::class.java)
val variableOne= txtVariableOne.text.toString()
i.putExtra("VARIABLEONE",variableOne)
startActivity(i)
}
Activity TWO
btnNext.setOnClickListener {
val i = Intent(this, Activity3::class.java)
val VariableTwo = txtVariableTwo.text.toString()
i.putExtra("VARIABLETWO",variableTwo)
startActivity(i)
}
Activity THREE (This is what I need)
Log.d("DEBUG ", "Value: " + intent.getStringExtra("VARIABLEONE"))
Log.d("DEBUG ", "Value: " + intent.getStringExtra("VARIABLETWO"))
I need access VariableOne and VariableTwo from activity Three.
Thank
Activity TWO
btnNext.setOnClickListener {
val i = Intent(this, Activity3::class.java)
val VariableTwo = txtVariableTwo.text.toString()
//THIS WHAT YOU NEED:
i.putExtra("VARIABLEONE",intent.getStringExtra("VARIABLEONE"));
i.putExtra("VARIABLETWO",variableTwo)
startActivity(i)
}
Activity Two
override fun viewAllNotes() {
val intent : Intent = Intent(this, ViewAllNotesActivity::class.java)
intent .putExtra("VARIABLEONE",variableOne)
startActivityForResult(intent, VIEW_ALL_REQUEST_CODE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK){
if (requestCode == VIEW_ALL_REQUEST_CODE){
//get data here
}
}
}
}
Activity three
#Override
public void onBackPressed() {
Intent result = new Intent();
result .putExtra("VARIABLEONE",variableOne)
setResult(Activity.RESULT_OK, result);
this.finish();
}
Or else you can store the values using Shared-preference and get values any where in the project
I can say the same as we passing in java
There are many ways
From the one is, You can pass by intent when starting another activity which is proper when you think data at the time of starting the activity like bellow,
val intent = Intent(mActivity, LoginActivity::class.java);
intent.putExtra("keyName", data)
startActivity(intent)
Another way you can use preference or store data in preference and you can use where ever you need.
I've just been coding in kotlin for a while. I've got some problems.
It always return null data in after I click item in second activity.
first activity
btnClick.setOnClickListener { v ->
val intent = Intent(applicationContext, NumberPickerActivity::class.java)
startActivityForResult(intent, 777)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
try {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 777 && resultCode == Activity.RESULT_OK) {
val result = data?.getStringExtra("picked_product").toString()
Toast.makeText(applicationContext, result, Toast.LENGTH_SHORT).show()
}
} catch (e: Exception) {
Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
}
}
second activity
override fun onItemClick(item: Product) {
val intent = Intent()
intent.putExtra("picked_product", item.price)
setResult(Activity.RESULT_OK, intent)
finish()
}
Because you are expecting an Int, do this instead:
val result = data?.getIntExtra("picked_product", 0) //0 will be used in case no value in data and result is now Integer.
The extra you put in your intent is an Integer (item.price). But you are trying to retrive a String data?.getStringExtra("picked_product").
Sinc the intent does not contain a String at the key "picked_product", it returns null.
You should try to get an Int extra :
val result = data?.getIntExtra("picked_product")
Nothing to do with your problem but it's useless to do
data?.getStringExtra("picked_product").toString()
Since it return you a String the use of toString() is useless