3. Data transfer between Android based activities

Keywords: Android Java xml ButterKnife

Zero, preface

Open the frontactivity, open the ToActivity through the button to return the result, and at the same time add the data to the intent,
Fill TextView with data in onCreate method of ToActivity.
Press the return button to pass the ToActivity data to FromActivity, verify the returned result in onActivityResult method and fill the data on TextView.

1, Java classes

FromActivity.java
public class FromActivity extends AppCompatActivity {

    private static final int DATA_CODE = 0x0001;

    @BindView(R.id.btn_for_result)
    Button mBtnForResult;
    @BindView(R.id.tv_result)
    TextView mTvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_from);
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case DATA_CODE:
                if (resultCode == RESULT_OK) {
                    String dataFormTarget = data.getStringExtra("data");

                    Bundle personData = data.getBundleExtra("To");
                    Person person = (Person) personData.get("person");
                    mTvResult.setText("dataFormTarget:" + dataFormTarget
                            + "\nperson:" + person.toString());
                }
                break;
        }

    }

    @OnClick({R.id.btn_for_result})
    public void onViewClicked(View view) {
        Intent intent = new Intent(this, ToActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("person", new Person("form", 23));
        intent.putExtra("from", bundle);
        startActivityForResult(intent, DATA_CODE);
    }
}
ToActivity.java
public class ToActivity extends AppCompatActivity {

    @BindView(R.id.btn_send)
    Button mBtnSend;
    @BindView(R.id.tv_to)
    TextView mTvTo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ac_target);
        ButterKnife.bind(this);

        Intent intent = getIntent();
        Bundle extra = intent.getBundleExtra("from");
        Person from = (Person) extra.get("person");
        mTvTo.setText(from.toString());

    }

    @OnClick(R.id.btn_send)
    public void onViewClicked() {
        backWithData();
        finish();
    }

    private void backWithData() {
        Person jt = new Person("JIT", 24);

        Intent intent = new Intent();
        intent.putExtra("data", "I am ToActivity Data");

        Bundle bundle = new Bundle();
        bundle.putSerializable("person", jt);
        intent.putExtra("To", bundle);
        setResult(RESULT_OK, intent);
    }

    /**
     * Override return key
     */
    @Override
    public void onBackPressed() {
        backWithData();
        super.onBackPressed();
    }
}

appendix

Person.java
public class Person implements Serializable {

    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = 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;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
ac_from.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_for_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="112dp"
        android:layout_marginTop="32dp"
        android:text="StartTargetForResult"
        android:textAllCaps="false"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_result"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click the button to get the return result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>
ac_to.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btn_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dp"
        android:layout_marginStart="30dp"
        android:layout_marginTop="24dp"
        android:text="Return to previous Activity Result"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:id="@+id/tv_to"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Result"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

Postscript

1. statement:

1. This article was originally written by Zhang fengjietelie. Please indicate for reprint
2. Welcome all programming enthusiasts to communicate with each other
3. Personal ability is limited. If there is anything wrong, you are welcome to criticize and testify. You must correct it with modesty
4. See here, thank you for your love and support

2. Connecting the transfer door:

More Android technologies welcome to: Android technology stack
My github address: Welcome star
Zhang Fengjie's personal website:http://www.toly1994.com

3. contact me

QQ:1981462002
Email: 1981462002@qq.com
Wechat: zdl1994328

Posted by Deany on Sat, 21 Dec 2019 09:23:21 -0800