Home >Java >javaTutorial >How to Pass Data Through an Intent Using Serializable in Android?

How to Pass Data Through an Intent Using Serializable in Android?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-29 22:26:10631browse

How to Pass Data Through an Intent Using Serializable in Android?

Passing Data through Intent using Serializable

To pass data through an Intent using Serializable, the class containing the data must implement the Serializable interface.

Consider the following implementation:

package com.ursabyte.thumbnail;

import java.io.Serializable;

import android.graphics.Bitmap;

public class Thumbnail implements Serializable {

    private static final long serialVersionUID = 1L;
    private String label = "";
    private Bitmap bitmap;

    // Constructor, getters, and setters omitted for brevity
}

To pass an instance of this class through an Intent, you can use a Bundle:

Bundle bundle = new Bundle();
bundle.putSerializable("value", thumbnail);
intent.putExtras(bundle);

In the receiving Activity, you can retrieve the data as follows:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();

Thumbnail thumbnail = (Thumbnail)bundle.getSerializable("value");

Note that the Bundle.putSerializable() method takes an Object as its argument, so you can pass any object that implements the Serializable interface.

Troubleshooting

If you are not able to pass data through an Intent using Serializable, here are a few things to check:

  • Ensure that the class containing the data implements the Serializable interface.
  • Use a Bundle to pass the data, as shown above.
  • Ensure that the receiving Activity retrieves the data using Bundle.getSerializable().

The above is the detailed content of How to Pass Data Through an Intent Using Serializable 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