Vector
Vector

Reputation: 3235

kotlin try catch block with bundle.getString

We have intent extras that are passed back to an Activity
The code is written with Kotlin 1.3 and is posted below
We do not understand why the code needs to be in a try catch block
Our question is there a better way to write this code and can someone explain why the code requires a try catch block. We know it could be written with when.
The navigation back to this Activity is accomplished with various intents that do not always put all the values that the bundle gets.
One button uses this code

    val intent = Intent(this,MainActivity::class.java)
    intent.putExtra("FROM", "NEW")
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
    startActivity(intent)

While another button uses this code

        holder.ivEdit.setOnClickListener {

        //val rowid = friendList.get(position).id
        val intent = Intent(context, MainActivity::class.java)
        intent.putExtra("FROM", "UPDATE")
        intent.putExtra("recordID", items.id)
        intent.putExtra("PERSON", items.person)
        intent.putExtra("PHONE", items.phone)
        intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
        context.startActivity(intent)
    }

Here is the code that is in the Activity that has the try catch code
The code is inside the onCreate function

        try {
        val bundle: Bundle = intent.extras
        from = bundle.getString("FROM","")
        txtPerson = bundle.getString("PERSON","")
        txtPhone = bundle.getString("PHONE","")

        if(from == "UPDATE") {
            showMSG("To CANCEL use back button")
            id = bundle.getInt("recordID", 4)
            btnAdd.visibility = View.INVISIBLE
            btnEdit.visibility = View.VISIBLE
            btnViewList.visibility = View.INVISIBLE
            etPerson.setText(txtPerson)
            etPhone.setText(txtPhone)

        }else if (from == "DELETE"){

            showMSG("To CANCEL use back button")
            btnAdd.visibility = View.INVISIBLE
            btnViewList.visibility = View.INVISIBLE
            btnEdit.visibility = View.INVISIBLE
            btnDelete.visibility = View.VISIBLE
            etPerson.setText(txtPerson)
            etPhone.setText(txtPhone)
            etPerson.isEnabled = false
            etPhone.isEnabled = false

        }else{

            btnViewList.visibility = View.VISIBLE
            btnAdd.visibility = View.VISIBLE
            btnEdit.visibility = View.INVISIBLE
        }
        if (id != 0) {
            //etPerson.setText(txtPerson)
            //etPhone.setText(txtPhone)
        }
    } catch (ex: Exception) {
    }

Upvotes: 0

Views: 231

Answers (1)

James_Duh
James_Duh

Reputation: 1371

The guess here is that the Activity with the try catch is also navigated to by another activity that passes no information for the bundle so the bundle gets set to null

intent.extras must not be null so if it is null you need a way to deal with that fact
I do not see a better way around the issue than the try catch block
perhaps someone can offer another solution.

Upvotes: 1

Related Questions