skip to Main Content

I make and Android App in Android Studio, and I have some problems with passing Float array from one Activity to another (I wanna pass this array and make a chart with MPAndroidChart). I found here how to pass a String Array (using Java) and tried to do the same with Float array (using Kotlin), but Android Studio returns an error. Please show how to pass the Float array to another Activity right, putFloatArrayExtra function doesn’t work in my project

I tried to use this code:

var b = Bundle();
b.putStringArray("key", myArray);
Intent i=new Intent(this, ChartActivity::class.java);
i.putExtras(b);

and this:

var b: Bundle = this.getIntent().getExtras();
var array=b.getStringArray(key);

2

Answers


  1. You have to use Intent#putExtra(String name, @Nullable float[] value) to put float array to the extras.

    And in your second Activity get this array using Intent#getFloatArrayExtra(String name):

        // In your Activity1
        val intent = Intent().apply {
            putExtra("key_float_array", FloatArray(1) { 0.0F })
        }
        startActivity(this, intent)
    
        // In your Activity2:
        val array = this.getIntent().getFloatArrayExtra("key_float_array")
    
    Login or Signup to reply.
  2. To pass an array from one intent to another in Kotlin for Android, you can follow this approach:

    In the first activity, declare and initialize the float array, Then create an intent and add the float array as an extra:

    val floatArray = floatArrayOf(1.0f, 2.0f, 3.0f) // Your Float Array
    
    val intent = Intent(this, SecondActivity::class.java)
    intent.putExtra("floatArray", floatArray)
    startActivity(intent)
    

    In the second activity, retrieve the float array from the intent:

    val floatArray = intent.getFloatArrayExtra("floatArray")
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search