Bundle Passes Data and Objects in Android

1.Bundle delivers data because simple data types are already encapsulated in Bundle, so let's set up the data directly. Let's see what we can do next:

  case R.id.Btn_Msg:
                // Instantiate a Bundle  
                Bundle bundle = new Bundle();
                Intent intent=new Intent(MainActivity.this,Main2Activity.class);
                //Set up data
                String name="admin";String num="123";
                //Save data in Bundle  
                bundle.putString("name", name);
                bundle.putString("num",num);
                //Put bundle s in intent  
                intent.putExtra("Message",bundle);
                startActivity(intent);
                break;

Put the data in a Bundle and then use intent to transfer it. Here's how to get the data transferred from a Bundle:

 //get data  
        Intent intent = getIntent();
        //Remove bundle from intent  
        Bundle bundle = intent.getBundleExtra("Message");
        //get data  
        String name = bundle.getString("name");
        String num = bundle.getString("num");
        //Display data  
        text_show.setText(name + "\n" + num);

2, Bundle passes objects, if we want to pass a complex data type we will use the method Serizlizable in Bundle
Here we're going to turn the data into a Serizlizable object and do the appropriate things

An object like this:

public class Persion implements Serializable {
    private String name;
    private String num;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }
}

Here's how to do it. This side sets the data to death and begins to pass the object

  case R.id.Btn_Obj:
                Persion persion=new Persion();
                //Set up data
                String Name="zhangsan";String Num="111111";
                persion.setName(Name);
                persion.setNum(Num);
                // Instantiate a Bundle  
                Bundle bundle1 = new Bundle();
                // Put Persion data into bundle s  
                bundle1.putSerializable("persion",persion);
                Intent intent1=new Intent(MainActivity.this,Main2Activity.class);
                intent1.putExtras(bundle1);
                startActivity(intent1);
                break;

Similarly, I put an object in a Bundle and pass it using Intent. Here's how we get the object passed by a Bundle:

 Intent intent=getIntent();
        // Instantiate a Bundle  
        Bundle bundle=intent.getExtras();
        //Get data inside Persion  
        Persion persion= (Persion) bundle.getSerializable("persion");
        text_show.setText("Full name:"+persion.getName()+"\n"+"number:"+persion.getNum());

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!Just need to parse step by step,,,,, just like writing code, all need to do step by step, no step by step.

Posted by skyxmen on Mon, 04 May 2020 03:42:28 -0700