Home java Custom elements in Bundle | Android

Custom elements in Bundle | Android




How can I transfer the custom type items in Bundle. For example, I want to transfer List, but I do not understand how. I read the articles and so on, but after all attempts received only errors. Now I can not say what exactly. Can someone explain to me how to do it right?

Answer 1, Authority 100%

There are several options for sending a list list. The first way as the second is to fill the list of class objects that you need and send. When this class should be either Parcelable or Serializable , but it is better to use Parcelable. Example Serializable Class:

Public Class Person Implements Serializable {
  Private int id;
  Private String Name;
  Public Hospitals (int ID, String Name)
    this.id = id;
    this.name = Name;

Next you from this List do ArrayList:

list & lt; classobject & gt; Yourslist = ...;
ArrayList & LT; ClassObject & GT; arrlist = new arraylist & lt; & gt; (yourist.size ());
arrlist.addall (YourList);

and send via Bundle:

bundle bundle = new bundle ();
Bundle.PutSerializable ("ValuesArray", ArrList);

Second option – Convert your object in JSON and send to Bundle as String:

string jsonlist = gson.tojson (YourList);
Bundle bundle = new bundle ();
bundle.putstring (...);

But it costs to remember that Bundle has a 1MB restriction (Pruf ):

The Binder Transaction Buffer Has a Limited Fixed Size, Currently 1MB,
Which is shared by All Transactions in Progress for the Process.

Therefore, the third option will be without using Bundle in general. You need to create a class in which there will be the field of your list and after filling out this list in activity you must notice this list into a separate class. After the list is now and in another class, too, then you can take it from anywhere, including with target activity. To begin with, create a Singleton class:

Public Class Single {
  Private Static Final Single Instance = New Single ();
  Private Single () {}
  Public Static Single GetInstance () {

Create a field of your array and method in it to take this array and add data to it:

list & lt; youroBject & gt; MyList = New List ()
Public List & LT; YouRoBject & GT; GetArray () {
   Return this.Mylist;
Public Void AddtoArray (YouRoBject OBJ) {
   mylist.add (OBJ);

And now you can fly data:

sindle.getinstance (). getArray ()

and take the data:

sindle.getinstance (). Mylist

Here is an example Work with Singleton. If you need to add the entire list immediately and not one element, then you need to use AddALL () . Here is Documentation About List. Well, the last option is to use SharedPreference . This method is not entirely good for such purposes, but the task can also be performed:

set & lt; string & gt; Tasksset = New Hashset & lt; String & GT; (objlist);
PreferenceEmanager.GetDefaultSharedPreferences (Context)
  .edit ()
  .putstringset ("Tasks_set", Tasksset)
  .commit ();

and read it where you need:

set & lt; string & gt; Tasksset = PreferenceEmanager.GetDefaultSharedPreferences (Context)
  .getstringset ("tasks_set", new hashset & lt; string & gt; ());
List & lt; String & GT; TasksList = New ArrayList & LT; String & GT; (Tasksset);

Also in SharedPreferences, you can record JSON String (convert because I showed above) and take where you need. Record:

sharedpreferences settings = context.getsharedpreference ("My_Prefs", context.mode_private);
Sharedpreferences.Editor Editor = settings.edit ();
editor.putstring ("Some_Key", Value) .APPly ();

and read where you need:

sharedpreferences settings = context.getsharedpreference ("My_Prefs", context.mode_private);
String Data = settings.getString ("Some_Key", "DefaultValue");

Answer 2, Authority 50%

@andrew forgot only to specify (mentioned Solz) about a regular option with Parcelable .

Meanwhile, this is a regular (native for Android) option that, by virtue of its implementation, provides very fast transmission and without using serialization and related restrictions, in particular in the documentation it is written that:

Parcel Is Not a Generalization Mechanism. This Class (and the correpsponding parcelable API for Placing Arbitrary Objects INTO A PARCEL) Is Designed as a High-Performance IPC Transport.

That is, essentially Parcelable is a special data transfer protocol between different processes, which relies on low-level implementation using the features of the Android OS kernel.

To use this protocol, you need your class to implement the Parcelable interface with the static field Creator , then as usual: Putparcelable () / getparcelable ()

For comparison, the effectiveness of Parcelable VS. Serializable

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions