3.安卓基礎之Activity間的數據傳遞

零、前言

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

Activity數據傳遞.gif

1、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.鏈接傳送門:

更多安卓技術歡迎訪問:安卓技術棧
個人github地址:歡迎star
張風捷特烈我的網站:http://www.toly1994.comjava

3.聯繫我
QQ:1981462002 郵箱:1981462002@qq.com 微信:zdl1994328
相關文章
相關標籤/搜索