recyclerview is not initialized when the activity is reloaded

31 views Asked by At

I'm new to android application development and I appreceiate some help.

there is a recycler view in my activity which is initialized in a handler which notifies the activity about the bluetooth changes.

when the activity is loaded for the first time, the recycler view initialization takes place without a problem, but after revisiting the initialization does not happen.

here is the code:

 private val bluetoothHandler: Handler = object: Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

            Log.d("add_actuator", "the main looper is: ${mainLooper.thread}")


            when(msg.what) {
                GlobalVariables.BLUETOOTH_STATE_LISTENING -> {
                    Log.d("add_actuator", "bluetooth status: listening")
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTING -> {
                    Log.d("add_actuator", "bluetooth status: connecting")
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTED -> {
                    Log.d("add_actuator", "bluetooth status: connected")
                    val jsonObjectToSend = JSONObject()
                    jsonObjectToSend.put("busAddr", "")
                    jsonObjectToSend.put("receiveRequest", jsonDataRequest)
                    jsonObjectToSend.put("stroke", noStrokeInJson)
                    val jsonString = jsonObjectToSend.toString()
                    val jsonByteArray = jsonString.toByteArray(StandardCharsets.UTF_8)
                    sendReceive.write(jsonByteArray)
                }

                GlobalVariables.BLUETOOTH_STATE_CONNECTION_FAILED -> {
                    Log.d("add_actuator", "bluetooth status: connection failed")
                }

                GlobalVariables.BLUETOOTH_STATE_MESSAGE_RECEIVED -> {
                    Log.d("add_actuator", "bluetooth status: msg received")
                    val incomingValue = msg.obj as ByteArray
                    val receivedMessage = String(incomingValue, 0, msg.arg1)
                    Log.d("add_actuator", "received message is: $receivedMessage")

                    jsonObject = JSONObject(receivedMessage)
                    for (key in jsonObject!!.keys()) {
                        Log.d("add_actuator", "the $key is: ${jsonObject!!.get(key)}")
                    }

                    // extracting information from thee json file
                    hubName = jsonObject?.get("hub_name").toString()



                    actuatorListObject = jsonObject!!.getJSONObject("actuators")

                    // Iterate through all keys (array names) in the JSON object
                    val keys = jsonObject!!.keys()
                    while (keys.hasNext()) {
                        val key = keys.next() as String
                        val value = jsonObject!!.get(key)

                        if (value is JSONArray) {
                            // Check if the value is a JSON array
//                            Log.d("Received_Message", "value is $value")

                            val bus_address = value[0].toString()
                            val board_number = value[1].toString()
                            val hex_number = value[2].toString()
                            val current_impulse = value[3].toString()
                            val max_impulse = value[4].toString()
                            val status = value[5].toString()

                            val actuatorModeList = listOf(
                                board_number,
                                hex_number
                            )
                            for (_key in calculationType.keys) {
                                if (_key == actuatorModeList) {
                                    val calcClass = calculationType.getValue(_key)

                                    addActuator(
                                        bus_address, board_number, hex_number,
                                        calcClass, current_impulse, max_impulse, false, hubName!!,
                                        "Simon Protec Actuator", status
                                    )

                                    // adding the bus address to the set for further depiction in
                                    // the recycler view
                                    actuatorBusAddrSet.add(bus_address)
                                }
                            }

//                            // Iterate through the elements of the JSON array
//                            for (i in 0 until value.length()) {
//                                val element = value.get(i)
////                                Log.d("Received_Message", "element is $element")
//                            }
//                        }
                        }
                    }

                    for(addr in actuatorBusAddrSet)
                        Log.d("add_actuator", "bus address in the set: $addr")

                    recyclerViewInitialization()
                }
                GlobalVariables.BLUETOOTH_STATE_DISCONNECTED ->{
                    Log.d("add_actuator", "bluetooth status in handler: disconnected")
                }
            }
        }
    }// bluetoothHandler

here is the recycler view initialization:

fun recyclerViewInitialization(){

        val itemAdapter = AddActuatorRecyclerViewAdapter(this, actuatorBusAddrSet)
        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view_add_actuator)

        recyclerView?.layoutManager = LinearLayoutManager(this)
        recyclerView?.adapter = itemAdapter

        recyclerView?.setHasFixedSize(true)
        itemAdapter?.notifyDataSetChanged()

    }// recyclerViewInitialization()

I tried to use another handler which runs another thread, which I can simply quit in onStop mehtod of the activity, but nothing happened and the problem persists.

1

There are 1 answers

0
Tayfun YILDIRIM On

You could add your bluetoothHandler variable inside onResume() method of activity. I assume you put in your activity's onCreate() method.

It looks like problem about activity lifecycle. You could find documentation about activity lifecycle here.