I have a rotating carousel (using carouselrecyclerview) to rotate a list of images around.
The user can call a second activity and then search the images, and then return the selected image’s ID back to the MainActivity.kt.
The MainActivity.kt receives the result (using intent) in the onActivityResult. I then need to call scrollToPosition (from the carouselLayoutManager) to move the carousel to the position that was selected in the second activity.
As the call to the carouselLayoutManager is within the onCreate, I can’t call it from the onActivityResult? I have tried moving the onActivityResult to within the onCreate, but then the onActivityResult is not called when returning from the second activity.
So, how can I call the code which is within the onCreate from the onActivityResult please?
Any help really appreciated as I’m struggling on this.
MainActivity.kt ...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val carouselRecyclerview = findViewById<CarouselRecyclerview>(R.id.recycler)
val list = ArrayList<DataModel>()
// Load the images of the Veg
for (mycount in 0..41) {
list.add(DataModel(VegName[mycount].image, VegName[mycount].name))
}
val adapter = DataAdapter(list)
carouselRecyclerview.adapter = adapter
val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
carouselLayoutManager.scrollToPosition(1)
carouselRecyclerview.setItemSelectListener(object : CarouselLayoutManager.OnSelected {
override fun onItemSelected(position: Int) {
var ShowIt = findViewById(R.id.textVegName) as TextView
//Cente item
ShowIt.text = list[position].text
}
})
Searchbutton.setOnClickListener {
val intent = Intent(this, SearchActivity::class.java)
startActivityForResult(intent, SEARCH_ACTIVITY_REQUEST_CODE)
}
// Move the carousel to the position received - THIS ISN'T CALLED?
fun setthelocation(SetThisPlace: Int ) {
carouselLayoutManager.scrollToPosition(SetThisPlace)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SEARCH_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
val returnedfrom = VegName.find{ it.name == data?.getStringExtra("result") }
if (returnedfrom==null)
Toast.makeText(applicationContext, "Did not find the result returned!", Toast.LENGTH_LONG).show()
else {
Toast.makeText(applicationContext, "Got = " returnedfrom.id, Toast.LENGTH_LONG).show()
//Need to call eiter setthelocation() or carouselLayoutManager.scrollToPosition???
return
}
}
}
}
CodePudding user response:
As the call to the carouselLayoutManager is within the onCreate, I can’t call it from the onActivityResult?
I think you're confusing terms. There is no "call to the carouselLayoutManager" - that's a variable to assign to an object, not a function you call.
I have tried moving the onActivityResult to within the onCreate, but then the onActivityResult is not called when returning from the second activity.
onActivityResult
is a base-class method that is invoked for you when you use startActivityForResult
and close the opened activity. If you "move it to within the onCreate" all you're doing is calling the base class implementation (which does nothing).
So, how can I call the code which is within the onCreate from the onActivityResult please?
The easiest solution would be to hold on to the layout manager as a variable you can use in either onCreate
or onActivityResult
:
// Class-level property you can use in either function
private lateinit var carouselLayoutManager: LayoutManager
override fun onCreate(...) {
// Replace local val with class-level property
// Instead of this:
// val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
// Do this: initialize member property to use here and in onActivityResult
carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == SEARCH_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
val returnedfrom = VegName.find{ it.name == data?.getStringExtra("result") }
if (returnedfrom==null)
Toast.makeText(applicationContext, "Did not find the result returned!", Toast.LENGTH_LONG).show()
else {
Toast.makeText(applicationContext, "Got = " returnedfrom.id, Toast.LENGTH_LONG).show()
//Need to call eiter setthelocation() or carouselLayoutManager.scrollToPosition???
// Now you can call this since it's a member property
carouselLayoutManager.scrollToPosition(...)
return
}
}
}
}
CodePudding user response:
hm, for this I would recommend you to use the NavComponent and send your data through fragments or registering for activity but I don't want to confuse you and I will try to give you a solution for this problem.
I think the easiest way to solve this (in this context) would be to launch the intent as you are doing:
MainActivity.kt ->
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val carouselRecyclerview = findViewById<CarouselRecyclerview>(R.id.recycler)
val list = ArrayList<DataModel>()
// Load the images of the Veg
for (mycount in 0..41) {
list.add(DataModel(VegName[mycount].image, VegName[mycount].name))
}
val adapter = DataAdapter(list)
carouselRecyclerview.adapter = adapter
val carouselLayoutManager = carouselRecyclerview.getCarouselLayoutManager()
carouselLayoutManager.scrollToPosition(1)
carouselRecyclerview.setItemSelectListener(object : CarouselLayoutManager.OnSelected {
override fun onItemSelected(position: Int) {
var ShowIt = findViewById(R.id.textVegName) as TextView
//Cente item
ShowIt.text = list[position].text
}
})
Searchbutton.setOnClickListener {
val intent = Intent(this, SearchActivity::class.java)
startActivity(intent)
}
fun setTheLocation(SetThisPlace: Int ) {
carouselLayoutManager.scrollToPosition(SetThisPlace)
}
// onNewIntent would receive the intent needed to execute your logic.
// I wouldn't use onActivityResult because, IMO, It is dirty code and it is deprecated.
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (intent?.hasExtra("position") == true) {
setTheLocation(intent.getIntExtra("position"))
}
}
SearchActivity.kt ->
override fun onCreate(savedInstanceState: Bundle?) {
...
.... your code ...
exampleOfSendingBackFunction()
}
//Here you will send back the position to MainActivity.kt clearing all flags.
fun exampleOfSendingBackFunction() {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
putExtras(
"position" to yourPositionVariable
)
}
startActivity(intent)
}
I hope it helps :D