0

I am trying to make connection to another application and I believe is working kind of. When press my button it shows me the logo of the other application. I am sending a package name and request code in

startActivityForResult(intent, 101)

This is my current code for when button is pressed.

        etSubmit.setOnClickListener {
            if (etCommand.text.isEmpty()) {
                Toast.makeText(this, "Komento ei voi olla tyhjä", Toast.LENGTH_SHORT).show()
            } else {
                Toast.makeText(this, "Komento lähetetty", Toast.LENGTH_SHORT).show()
                val customAction = "com.app.app.DevicesList"
                val intent = Intent(customAction)
                startActivityForResult(intent, 101)
                //startActivity(intent)
            }
        }

This is the xamarin c# version of the code.

string customAction = "com.app.app.DevicesList"; 
var intent = new Intent(); 
intent.SetAction(customAction); 
StartActivityForResult(intent, 101);

I get the other application logo once but when I press the button again I do not get the logo again.

Then I am trying to display something to the user by using the following function.

    private fun onActivityResult() {
        val requestCode: Number = 0
        val mapper = jacksonObjectMapper()
        if (requestCode == 101) {
            var dl = data.GetStringExtra("DevicesList");
            var devices = Json.parseToJsonElement(dl).jsonObject
            //var devices = mapper.readValue<List<String>>(dl)
            devices.   .add(0, "Phone")
            devices.add(0, "(Default)")
            //var devices = Json.decodeFromString<List<String>>(dl);
            //devices.in   .Insert(0, "Phone");
            //devices.Insert(0, "(default)");
        }
    }

and this is the xamarin version.

if (requestCode == 101) {
var dl = data.GetStringExtra("DevicesList");
var devices = JsonConvert.DeserializeObject<List<string>>(dl); 
devices.Insert(0, "Phone");
devices.Insert(0, "(default)");
}

I am not exactly sure how to handle/or do this in kotlin.

This how I have it now currently

    private fun openActivityForResult() {
        val customAction = "com.Moasure.Moasure.DevList"
        val intent = Intent(customAction)
        resultLauncher.launch(intent)
    }

    private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
        if (result.resultCode == Activity.RESULT_OK) {
            // There are no request codes
            val data: Intent? = result.data

            var dl = data?.getStringExtra("DevList");

            var devices = Json.parseToJsonElement(dl).jsonObject;
            //var devices = dl?.let { mapper.readValue<List<String>>(it) };
            devices.add(0, "Phone")
            devices.add(0, "(default)")
            Log.d(devices.toString(), "devices list #################")
        }
    }

This is my latest attempt.

    private fun openActivityForResult() {
        val customAction = "com.Moasure.Moasure.DevList"
        val intent = Intent(customAction)
        onActivityResult(101, 101, intent)
    }
    
       private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->

        if (result.resultCode == Activity.RESULT_OK) {
            // There are no request codes
            val data: Intent? = result.data
            data?.putExtra("data", data)
            Log.d(data.toString(), "data: ####")

            if (result.resultCode === Activity.RESULT_OK) {
                var dl = data?.getStringExtra("DevList");
                if (dl != null) {
                    Log.d(dl.length.toString(), "devices list: ####")
                    val mapper = jacksonObjectMapper()
                    var devices = Json.parseToJsonElement(dl.toString()).jsonObject;
                    //var devices = dl?.let { mapper.readValue<List<String>>(it) };
                    //var devices = JsonConvert.DeserializeObject<List<String>>(dl);
                    //devices.putExtra(0, "Phone");
                    //devices.putExtra(0, "(default)");
                    Log.i(devices.toString(), "### Devices")
                }
            }
        }
    }

Length is giving me d/2 in the logcat.

Jukka Koivu
  • 269
  • 4
  • 15

2 Answers2

0

you have probably misplaced onActivityResult method, it must have override at the beginning

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == 101) {
        var dl = data.GetStringExtra("DevicesList");
        var devices = Json.parseToJsonElement(dl).jsonObject
        //var devices = mapper.readValue<List<String>>(dl)
        devices.add(0, "Phone")
        devices.add(0, "(Default)")
        //var devices = Json.decodeFromString<List<String>>(dl);
        //devices.in   .Insert(0, "Phone");
        //devices.Insert(0, "(default)");
    }
}

overriding this method is possible only inside Activity, as this method belongs to it

btw. this way is deprecated now, check out what you can do for fixing warnings (use new way)

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • I updated my question with latest changes and there are still some erros and it only shows the other app once when starting the intent like it does it only once. – Jukka Koivu Apr 27 '22 at 11:28
  • And now the devices.add() `.add ` is red giving erros – Jukka Koivu Apr 27 '22 at 11:43
  • I hope you have noticed that I've posted this line with two dots (so obvius mistype)... Fixed my answer – snachmsm Apr 28 '22 at 05:41
  • I am not sure if I did. I updated with my latest attempt now. But now the application crashes and getting following error. ```java.lang.IllegalArgumentException: Element class kotlinx.serialization.json.JsonNull is not a JsonObject``` – Jukka Koivu Apr 28 '22 at 07:44
0

It happens to be that code was working after all but I just needed to register to the other application to get the device showing.

Jukka Koivu
  • 269
  • 4
  • 15