Home  >  Article  >  Java  >  How to Send and Receive an Integer Array via Intent in Android?

How to Send and Receive an Integer Array via Intent in Android?

DDD
DDDOriginal
2024-10-27 20:09:30396browse

How to Send and Receive an Integer Array via Intent in Android?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn