详解Android中Intent传递对象给Activity的方法

2019-12-10 18:14:10于丽

接收方activity的layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
 
  <TextView
    android:id="@+id/textview1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
 
</LinearLayout>

接收方activity:

package com.app.main;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class NextMain extends Activity {
 
  @Override
  public void onCreate(Bundle savedInstanceState) {
 
    super.onCreate(savedInstanceState);
 
    this.setContentView(R.layout.nextmain);
 
    TextView tv = (TextView) this.findViewById(R.id.textview1);
 
    Intent intent = this.getIntent();
 
    if (intent.getExtras().get("person") != null) {
 
      Person p = (Person) intent.getExtras().get("person");
 
      tv.setText("name:" + p.getName() + ",age:" + p.getAge()
          + ",gender:" + p.getGender());
 
    }
  }
}

效果如下: 

Android,Intent,Activity

Android,Intent,Activity