0

I'm facing following Errors/Exceptions when receiving data from one App to another App in Kotlin (Android):

  1. Class Not Found When Unmarshalling: com.example.Parcelable.User
  2. mExtras=Bundle[{)}]
  3. mParcelledData=null
  4. mMap=size 0

Also, In MainActivtiy: if(intent.hasExtra("USER")) statement giving error and displaying Toast message written in Else statement.

Please help me how I can resolve these exceptions ? Thanks a Lot!

(SendingApp File) -> MainActivity.kt

btnSendData1.setOnClickListener {
            val intent = Intent(Intent.ACTION_SEND).setType("text/plain")

            intent.component =
                ComponentName.unflattenFromString("com.example.receive")

            val args = Bundle()
            args.putParcelable(USER_KEY, User("Ahmer", "28"))
            intent.putExtras(args)

            Log.d("Ahmer", args.toString())

            startActivity(Intent.createChooser(intent, "Share to..."))
        }

(ReceivingApp File) -> MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var textView: TextView

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textView = findViewById(R.id.tv_username)

        when (intent?.action) {
            Intent.ACTION_SEND -> {
                if ("text/plain" == intent.type) {
                    handleSendText(intent)
                } else {
                    Toast.makeText(this, "No Text", Toast.LENGTH_SHORT).show()
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    private fun handleSendText(intent: Intent) {
        if (intent.extras != null) {
            if (intent.hasExtra("USER")) {
                Log.d("Intent", intent.toString())

                val bundle = intent.extras
                val user = bundle?.getParcelable<User>("USER") as User
                textView.text = "${user.name}${user.age}"
            } else {
                Toast.makeText(this, "Has Extras Null", Toast.LENGTH_SHORT).show()
            }
        } else {
            Toast.makeText(this, "Extras Null", Toast.LENGTH_SHORT).show()
        }
    }
}

(ReceivingApp File) -> User.kt

package com.example.receive

import android.os.Parcelable
import kotlinx.android.parcel.Parcelize

@Parcelize
data class User(
    var name: String,
    var age: String
) : Parcelable
  • 2
    Passing custom types between apps is unlikely to work well. Pass the data in a neutral format, such as JSON. – CommonsWare Jun 29 '22 at 16:26
  • This doesn't apply to your specific question but it is in the same ballpark. If binding is required (it doesn't seem to be in this case), then you could use AIDL and share the AIDL file between both apps. This will allow you to essentially share custom data types. –  Jun 29 '22 at 20:53

0 Answers0