Pass parameters between activities

1. Pass simple data

2. Pass the data package Bundle

3. Passing value objects

4. Get the return parameters of the Activity

 

1. Pass simple data

How to pass simple data when Activity jumps.

 

The idea of ​​passing data when starting an activity is very simple. Intent provides a series of overloads of the putExtra() method, which can temporarily store the data we want to pass in the Intent. After starting another activity, we only need to put these data It can be removed from the Intent again.

 

Add a Button in activity_main.xml and change the layout to LinearLayout

New hello.xml

Put a TextView inside that doesn't display anything

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:id="@+id/textView_1" />

MainActivity.java

 

The putExtra() method receives two parameters, the first parameter is the key, which is used to retrieve the value from the Intent later, and the second parameter is the actual data to be passed.

 

public class MainActivity extends AppCompatActivity {

    //declare variable
    private Button button_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Save the state of the Activity
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        // get object reference
        button_1=(Button)this.findViewById(R.id.button_1);

        //Register listener, anonymous inner class
        button_1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,hello.class);
                intent.putExtra("data","Hello jikexueyuan");//Pass string
                startActivity(intent);
            }
        });
    }
}

 Here to create a new hello.java

 

The Intent used to start hello is obtained through the getIntent() method, and then the getStringExtra() method is called, and the corresponding key value is passed in, and the passed data can be obtained. Here, since we are passing a string, we use the getStringExtra() method to get the passed data, if the passed integer data, use the getIntExtra() method, and if the passed Boolean data, use getBooleanExtra() method, and so on.

 

public class hello extends AppCompatActivity {

    private TextView textView_1;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView(R.layout.hello);

        textView_1=(TextView)findViewById(R.id.textView_1);
        Intent intent=getIntent();
        textView_1.setText(intent.getStringExtra("data"));
    }
}

AndroidManifest.xml

<activity
            android:name="hello"
            android:label="@string/app_name">
        </activity>

running result:

When button 1 is clicked:

 

2. Pass the data package Bundle

How to pass complex data when Activity jumps.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //declare variable
    private Button button_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Save the state of the Activity
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        // get object reference
        button_2=(Button)this.findViewById(R.id.button_2);

        //Register listener, anonymous inner class
        button_2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,new_main.class);
                Bundle bundle=new Bundle();
                bundle.putString("name","jikexueyuan");//Pass string
                bundle.putInt("age",2);//Pass integer type parameters
                bundle.putString("name1","iwen");
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
}

new_main.java

public class new_main extends AppCompatActivity {

    private TextView textView_2;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView(R.layout.new_main);

        textView_2=(TextView)findViewById(R.id.textView_2);
        Intent intent=getIntent();
        Bundle data=intent.getExtras();
        textView_2.setText(String.format("name=%s,age=%d,name1=%s",data.getString("name"),
data.getInt("age"), data.getString("name1", "leo")));//If name1 is not passed in, the default value leo is displayed
    }
}

 Another way:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //declare variable
    private Button button_2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Save the state of the Activity
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        // get object reference
        button_2=(Button)this.findViewById(R.id.button_2);

        //Register listener, anonymous inner class
        button_2.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,new_main.class);
                Bundle bundle=new Bundle();
                bundle.putString("name","jikexueyuan");
                bundle.putInt("age",2);
                bundle.putString("name1","iwen");
                //intent.putExtras(bundle);
                intent.putExtra("data",bundle);
                startActivity(intent);
            }
        });
    }
}

new_main.java

public class new_main extends AppCompatActivity {

    private TextView textView_2;
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView(R.layout.new_main);

        textView_2=(TextView)findViewById(R.id.textView_2);
        Intent intent=getIntent();
        //Bundle data=intent.getExtras();
        Bundle data=intent.getBundleExtra("data");
        textView_2.setText(String.format("name=%s,age=%d,name1=%s",data.getString("name"),data.getInt("age"),data.getString("name1","leo")));
    }
}

 

3. Passing value objects

How to pass a custom value object when Activity jumps.

Add a new button_3 in activity_main

New User.class

public class User implements Serializable{

    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

MainActivity.java

public class MainActivity extends AppCompatActivity {

    //declare variable
    private Button button_3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        //Save the state of the Activity
        super.onCreate (savedInstanceState);
        setContentView(R.layout.activity_main);

        // get object reference
        button_3=(Button) this.findViewById(R.id.button_3);

        //Register listener, anonymous inner class
        button_3.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,ding.class);
                intent.putExtra("user",new User("jikexueyuan",2));
                startActivity(intent);
            }
        });
    }
}

ding.java

public class ding extends AppCompatActivity{

    private TextView textView_3;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView(R.layout.ding);

        textView_3=(TextView)findViewById(R.id.textView_3);
        Intent intent=getIntent();

        User user=(User)intent.getSerializableExtra("user");
        textView_3.setText(String.format("User info(name=%s,age=%d)",user.getName(),user.getAge()));
    }
}

Another implementation method provided by the android platform

User.java

public class User implements Parcelable{

    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(getName());
        dest.writeInt (getAge ());
    }

    public static final Creator<User> CREATOR=new Creator<User>() {
        @Override
        public User createFromParcel(Parcel source) {
            return new User(source.readString(),source.readInt());
        }

        @Override
        public User[] newArray(int size) {
            return new User[size];
        }
    };
}

 ding.java

public class ding extends AppCompatActivity{

    private TextView textView_3;

    protected void onCreate(Bundle savedInstanceState){
        super.onCreate (savedInstanceState);
        setContentView(R.layout.ding);

        textView_3=(TextView)findViewById(R.id.textView_3);
        Intent intent=getIntent();

        //User user=(User)intent.getSerializableExtra("user");
        User user=intent.getParcelableExtra("user");
        textView_3.setText(String.format("User info(name=%s,age=%d)",user.getName(),user.getAge()));
    }
}

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326194840&siteId=291194637