Sending Arrays via Intent.putExtra
Within the Activity A, you have an array of integers that you want to transfer to Activity B. You create an intent and utilize the putExtra method for this purpose:
<code class="java">int[] array = {1, 2, 3}; Intent i = new Intent(A.this, B.class); i.putExtra("numbers", array); startActivity(i);</code>
However, upon receiving the information in Activity B, you encounter an issue:
<code class="java">Bundle extras = getIntent().getExtras(); int arrayB = extras.getInt("numbers");</code>
When you get the value from the intent, you're attempting to retrieve a single integer into arrayB, but what you actually have is an array of integers. To resolve this issue, you need to adjust your code in Activity B as follows:
<code class="java">int[] arrayB = extras.getIntArray("numbers");</code>
This change ensures that you correctly retrieve the array from the intent and have access to the individual integer values within it.
The above is the detailed content of How to Send and Receive an Integer Array via Intent in Android?. For more information, please follow other related articles on the PHP Chinese website!