Home >Java >javaTutorial >How to Properly Pass Serializable Data Between Android Activities?

How to Properly Pass Serializable Data Between Android Activities?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-23 21:37:11715browse

How to Properly Pass Serializable Data Between Android Activities?

Passing Data through Intent Using Serializable

Implement serializable to transfer data between Android components. However, if your implementation does not work despite marking your class as serializable, consider the following:

Ensure Proper Serializable Implementation

Your Thumbnail class should correctly implement the Serializable interface with a serialVersionUID. Ensure that all fields in the class are either transient or serializable.

Use Bundle.Serializable for Data Transfer

Instead of directly putting the serializable list into the intent, use Bundle.Serializable to pass it:

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

Retrieve Serializable Data in Receiving Activity

In the receiving activity, retrieve the serializable list using Bundle:

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

List<Thumbnail> thumbs =
               (List<Thumbnail>)bundle.getSerializable("value");

The above is the detailed content of How to Properly Pass Serializable Data Between Android Activities?. 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