55

I am trying to pass my own custom object into a bundle:

Bundle b = new Bundle();
                STMessage message = (STMessage)a.getAdapter().getItem(position);
                b.putObject("message",message);

I get the error:

The method putObject(String, Object) is undefined for the type Bundle
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

6 Answers6

63

One way is to have your custom object implement the Parcelable interface and use Bundle.putParcelable/Bundle.getParcelable

Jorgesys
  • 124,308
  • 23
  • 334
  • 268
Ryan Reeves
  • 10,209
  • 3
  • 42
  • 26
  • 7
    The documentation states that you should NOT use Serializable as your go-to parceling mechanism. Because it uses reflection, it's quite heavy on the system. See http://developer.android.com/reference/android/os/Parcel.html (look for writeSerializable) – dmon Apr 25 '11 at 23:34
  • No problem! It is weird that the docs don't mention this caveat in the Bundle one. – dmon Apr 25 '11 at 23:44
  • We can also use putSerializable which puts a serialized object into the bundle make your class serializable using Serializable interface – Deepak Yadav Jun 28 '18 at 17:25
23

Model Class

package com.sam.bundleobjectpass;

import java.io.Serializable;

/**
 * Created by Samir on 31.8.2016.
 */
public class Model implements Serializable {
    private String name;
    private String surName;
    private int age;

    public Model(String name, String surName, int age) {
        this.name = name;
        this.surName = surName;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    
    public String getSurName() {
        return surName;
    }


    public int getAge() {
        return age;
    }


}

MainActivity

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Model model = new Model("Sam", "Sami",32);
        
        Intent i = new Intent(MainActivity.this, ReceiverActivity.class);
        i.putExtra("Editing", model); // sending our object. In Kotlin is the same
        startActivity(i);

    }
}

ReceiverActivity

public class ReceiverActivity extends Activity {

    TextView txt_name;
    TextView txt_surname;
    TextView txt_age;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);

        txt_name = (TextView)findViewById(R.id.txt_name);
        txt_surname = (TextView)findViewById(R.id.txt_surname);
        txt_age = (TextView)findViewById(R.id.txt_age);
        // receiving our object
        Model model = (Model) getIntent().getSerializableExtra("Editing");

        txt_name.setText(model.getName());
        txt_surname.setText(model.getSurName());
        txt_age.setText(""+model.getAge());


    }
}

// Kotlin

val model: ProgramModel? = intent.getSerializableExtra("Editing") as ProgramModel?
        model?.let { // means if not null or empty
            txt_act_daily_topic.text = it.title
        }
Biscuit
  • 4,840
  • 4
  • 26
  • 54
Samir
  • 6,405
  • 5
  • 39
  • 42
22

Since using Parsable is designed for high performance IPC transport as mentioned in some of the comments, I tried using a different approach.

My approach uses GSON library by google.

Example

public class Person{
    private String name;
    private int age;
    // Getter and Setters omitted
}

You can have a method in utility class that returns Gson instance, this is for the sake of clean code and organisation. I will use GsonBuilder incase someone what to register custom adapter.

public class Utils {
    private static Gson gson;
    
    public static Gson getGsonParser() {
        if(null == gson) {
            GsonBuilder builder = new GsonBuilder();
            gson = builder.create();
        }
        return gson;
    }
}

Moment of truth!

PUT

Bundle args = new Bundle();
String personJsonString = Utils.getGsonParser().toJson(person);
args.putString(PERSON_KEY, personJsonString);

GET

Bundle args = getArguments();
String personJsonString = args.getString(PERSON_KEY);
Person person= Utils.getGsonParser().fromJson(personJsonString, Person.class);

Currently I don't know the performance limitation of this approach. But it works just fine

Emmanuel Mtali
  • 4,383
  • 3
  • 27
  • 53
9

Make your custom object Parcelable or Serializable then use putParcelable or putSerializable.

Depending on the complexity of your object one or other may be easier.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Something very important must be noted. Bundle does NOT do the actual parcelation or serialization. The `Parcel` class does. Whenever external system decides (for example activity cold recreation, on soft recreation that is simple rotation, parcelation might not take place at all). `Bundle` just caches the complex objects. One time I assumed the objects are *flattened* and this wrong assumption led to nasty leaks and 2 days to find them. – WindRider Sep 01 '16 at 18:33
0

As Ryan stated. Also if you only want to pass one object on a soft kill consider using onRetainConfigurationInstance and getLastNonConfigurationInstance to pass the serializable object. The entire object tree must be serializable. If you are passing the object via an intent, consider refactoring the object code to a Parceable code later, once the architecture has stabilized. Finally, consider using the fully qualified name of the class when storing the object into the Bundle as in:

inPWState= (PasswordState) getIntent().getSerializableExtra("jalcomputing.confusetext.PasswordState");

Hope that helps. JAL

JAL
  • 3,319
  • 2
  • 20
  • 17
0

It's work if you make your object class as Serializable

    class your_data_class implements Serializable