美文网首页
使用Intent传递对象(Parcelable方式)

使用Intent传递对象(Parcelable方式)

作者: 昨天剩下的一杯冷茶 | 来源:发表于2018-11-03 10:17 被阅读10次

第一个Activity布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    android:orientation="vertical"
    tools:context="com.example.hzx.intenttoclass.MainActivity">

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转到第二Activity"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="第一个界面"
         />

</LinearLayout>

第二个Activity布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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"
    tools:context="com.example.hzx.intenttoclass.Main2Activity"
    android:orientation="vertical">

    <TextView
        android:id="@+id/show_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

第一个Activity

public class MainActivity extends AppCompatActivity {

    private Button btn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn = (Button)findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Person person = new Person();
                person.setName("Tom");
                person.setAge(20);
                Intent intent = new Intent(MainActivity.this,Main2Activity.class);

                intent.putExtra("person_data",person);
                startActivity(intent);
            }
        });
    }
}

第二个Activity

public class Main2Activity extends AppCompatActivity {

    private TextView show_text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        //Person person = (Person)getIntent().getSerializableExtra("person_data");
        Person person = (Person)getIntent().getParcelableExtra("person_data");
        show_text = (TextView)findViewById(R.id.show_text);
        show_text.setText("name:"+person.getName()+"\n"+"age:"+person.getAge());
    }
}

效果和 “使用Intent传递对象(Serializable方式)”

总结:
使用Intetn来传递对象,可以使用Serializable和Parcelable。Serializable的方式较为简单,但是由于会把整个对象进行序列化,因此效率会比较Parcelable方式低一些,所以在通常情况下还是更加推荐使用Parcelable的方式来实现Intent传递对象的功能。

相关文章

网友评论

      本文标题:使用Intent传递对象(Parcelable方式)

      本文链接:https://www.haomeiwen.com/subject/xfqkxqtx.html