1. 程式人生 > >3.安卓基礎之Activity間的資料傳遞

3.安卓基礎之Activity間的資料傳遞

零、前言

開啟FromActivity,通過按鈕以返回結果方式開啟ToActivity,同時在intent中加入資料, 在ToActivity的onCreate方法中使用資料填充到TextView上。 按返回按鈕,將ToActivity資料傳遞給FromActivity,在onActivityResult方法中驗證返回結果並將資料填充到TextView上。

Activity資料傳遞.gif

一、Java類

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("捷特", 24);

        Intent intent = new Intent();
        intent.putExtra("data", "我是ToActivity的資料");

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

    /**
     * 重寫返回鍵
     */
    @Override
    public void onBackPressed() {
        backWithData();
        super.onBackPressed();
    }
}

Activity傳遞資料.png

附錄

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="點選按鈕 獲取返回結果"
        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="返回給上一個Activity結果"
        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="結果"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

後記、

1.宣告:
1.本文由張風捷特烈原創,轉載請註明 2.歡迎廣大程式設計愛好者共同交流 3.個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正 4.看到這裡,感謝你的喜歡與支援
2.連線傳送門:
3.聯絡我
QQ:1981462002 郵箱:[email protected] 微信:zdl1994328