Reputation: 1
I have a problem with onActivityResult()
and setResult()
.
I started android development 1 week ago so sorry.
Could you help me?
here is a snippet of my code :
ListActivity function Click
val myadapter = ItemAdapter();
list_recycler_view.layoutManager = LinearLayoutManager(this)
list_recycler_view.adapter = myadapter
val me = this;
myadapter.setOnItemClickListener(object : ItemAdapter.OnItemClickListener {
override fun onItemClick(item: Item) {
val intent = Intent(me, DetailActivity::class.java)
intent.putExtra("item", item)
startActivityForResult(intent, 2)
}
})
List activity function onActivityResult
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
Log.d("reponse","coucou5");
Log.d("reponse",requestCode.toString());
Log.d("reponse",resultCode.toString());
if (requestCode == 2 && resultCode == RESULT_OK) {
val item = data?.getParcelableExtra<Item>("item") as Item
Log.d("reponse-update",item.toString());
if (item == null) {
Toast.makeText(this, "Could not update! Error!", Toast.LENGTH_SHORT).show()
}
//ItemListViewModel.update(item);
} else {
Toast.makeText(this, "Note not saved!", Toast.LENGTH_SHORT).show()
}
}
DetailActivity function on click
btnSave.setOnClickListener{
//other props
item.gid = gid;
item.distance = distance
Log.d("reponse",item.toString());
Log.d("reponse","coucou4");
val returnIntent = intent
returnIntent.putExtra("item", item)
setResult(Activity.RESULT_OK, returnIntent)
finish()
}
it seems that the onActivityResult()
is never called
Upvotes: 0
Views: 248
Reputation: 1
it seems that i was declare 2 listener on the adapter so the startActivityForResult doesnt fire
Upvotes: 0
Reputation: 503
I think it is because you are not creating another intent to send the info back, please do this and check if that works for you.
Do this in your detail activity instead of your code just paste this.
btnSave.setOnClickListener{
//other props
item.gid = gid;
item.distance = distance
Log.d("reponse",item.toString());
Log.d("reponse","coucou4");
Intent returnIntent = Intent();
returnIntent.putExtra("item", item)
setResult(2, returnIntent)
finish()
}
you are passing RESULT_OK which isn't required and it's processed by the Android framework itself.
Upvotes: 1
Reputation: 519
Replace
val returnIntent = intent
For
val returnIntent = new Intent()
Upvotes: 0