Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass custom object in Bundle?

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 
like image 345
Sheehan Alam Avatar asked Apr 25 '11 23:04

Sheehan Alam


People also ask

Can we pass object in bundle?

Android Intent Passing custom object between activitiesIt is also possible to pass your custom object to other activities using the Bundle class.

How do objects pass through bundles?

To summarize, when using Bundle to pass objects, I first let the Persion class implement the Serializable interface, then use putSerializable(String key,Serializble value) to store the data, and then Serializanle getSerizlizable (String key) to fetch the data, which is very simple!


2 Answers

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

like image 117
Ryan Reeves Avatar answered Oct 02 '22 11:10

Ryan Reeves


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         } 
like image 31
Samir Avatar answered Oct 02 '22 11:10

Samir