Home > Java > javaTutorial > body text

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

DDD
Release: 2024-10-27 20:09:30
Original
478 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template