Android intent 传递对象以及返回刷新

之前项目需要,找过这方面知识。所以今天也总结一下。大家都知道activity跳转用intent,Android的当前页面跳转到新的页面。当然跳转的同时常常要携带数据或者对象。那我下面就说说跳转带对象吧。还有在例子当中,新的activity返回时,也有对象一起返回。下面看代码,只要入门Android了都看得懂,我就不再写很多注释了。

demo源码例子下载地址:点击此处下载

有两种对象类型可以传递,一是Parcelable,推荐用这个。二是Serializable。当然你在class建立对象时,都要对这两个进行接口。看代码。

package com.example.intentdemo;

import android.os.Parcel;
import android.os.Parcelable;

public class User implements Parcelable{

	private String name;
	private String sex;
	private int eage;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public int getEage() {
		return eage;
	}

	public void setEage(int eage) {
		this.eage = eage;
	}
	
	public static final Parcelable.Creator<User> CREATOR = new Creator<User>() {
		public User createFromParcel(Parcel source) {
			User user = new User();
			user.name = source.readString();
			user.sex = source.readString();
			user.eage = source.readInt();
			return user;
		}

		public User[] newArray(int size) {
			return new User[size];
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel parcel, int arg1) {
	     parcel.writeString(name);  
	     parcel.writeString(sex);  
	     parcel.writeInt(eage);  
	}

}

package com.example.intentdemo;

import java.io.Serializable;

public class Student implements Serializable {
	private String name;
	private String sex;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public int getEage() {
		return eage;
	}
	public void setEage(int eage) {
		this.eage = eage;
	}
	private int eage;
}

MainActivity.class

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

	private ArrayList<User> mArrayListUsers = new ArrayList<User>();
	private ArrayList<Student> mArrayListStudent = new ArrayList<Student>();

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

		InitUI();
	}

	private void InitUI() {
		Button Parcelable = (Button) findViewById(R.id.button1);
		Button Serializable = (Button) findViewById(R.id.button2);

		if (Parcelable != null) {
			Parcelable.setOnClickListener(this);
		}
		if (Serializable != null) {
			Serializable.setOnClickListener(this);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onClick(View arg0) {
		if (arg0.getId() == R.id.button1) {
			setParcelable();
		} else if (arg0.getId() == R.id.button2) {
			setSerializable();
		}

	}

	private void setSerializable() {
		Student student = new Student();
		student.setName("Serializable");
		student.setSex("nv");
		student.setEage(25);

		mArrayListStudent.add(student);
		mArrayListStudent.add(student);

		Intent mIntent = new Intent(this, IntentSerializableActivity.class);
		Bundle mBundle = new Bundle();
		mBundle.putSerializable("student_key", student); // 传递一个user对象
		mBundle.putSerializable("student_Arry", mArrayListStudent); // 传递一个user对象列表
		mIntent.putExtras(mBundle);

		startActivityForResult(mIntent, 200); // 带返回结果的Intent,标志码是1

	}

	private void setParcelable() {
		User user = new User();
		user.setName("Parcelable");
		user.setSex("nan");
		user.setEage(25);

		mArrayListUsers.add(user);
		mArrayListUsers.add(user);

		Intent mIntent = new Intent(this, IntentParcelableActivity.class);
		Bundle mBundle = new Bundle();
		mBundle.putParcelable("User_key", user); // 传递一个user对象
		mBundle.putParcelableArrayList("User_Arry", mArrayListUsers); // 传递一个user对象列表
		mIntent.putExtras(mBundle);

		startActivityForResult(mIntent, 100); // 带返回结果的Intent,标志码是1

	}

	// 返回activity页面刷新
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		TextView show = (TextView) findViewById(R.id.test);
		if (resultCode == RESULT_OK && requestCode == 100) {

			Bundle bundle = this.getIntent().getExtras();
			if (bundle != null) {
				mArrayListUsers = bundle.getParcelableArrayList("User_Arry");
			}

			show.setText("");
			show.append("IntentParcelableActivity返回的数据: " + "\n");
			for (int i = 0; i < mArrayListUsers.size(); i++) {
				User userArrayList = new User();
				userArrayList = mArrayListUsers.get(i);
				show.append("name is: " + userArrayList.getName() + "\n"
						+ "age is: " + userArrayList.getEage() + "\n");
			}

		} else if (resultCode == RESULT_OK && requestCode == 200) {

			Bundle bundle = this.getIntent().getExtras();
			if (bundle != null) {
				mArrayListStudent = (ArrayList<Student>) bundle
						.getSerializable("student_Arry");
			}

			show.setText("");
			show.append("IntentSerializableActivity返回的数据: " + "\n");
			for (int i = 0; i < mArrayListStudent.size(); i++) {
				Student studentArrayList = new Student();
				studentArrayList = mArrayListStudent.get(i);
				show.append("name is: " + studentArrayList.getName() + "\n"
						+ "age is: " + studentArrayList.getEage() + "\n");
			}

		}
	}

}

接受数据新的activity

IntentParcelableActivity.class

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;

public class IntentParcelableActivity extends Activity {
	private ArrayList<User> mArrayListUsers = new ArrayList<User>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		TextView mTextView = new TextView(this);

		User user = new User();
		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null) {
			user = bundle.getParcelable("User_key");
			mArrayListUsers = bundle.getParcelableArrayList("User_Arry");
		}

		mTextView.append("You name is: " + user.getName() + "\n"
				+ "You age is: " + user.getEage() + "\n" + "\n");

		for (int i = 0; i < mArrayListUsers.size(); i++) {
			User userArrayList = new User();
			userArrayList = mArrayListUsers.get(i);
			mTextView.append("You name is: " + userArrayList.getName() + "\n"
					+ "You age is: " + userArrayList.getEage() + "\n");
		}

		setContentView(mTextView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	// 返回按键调用
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			Bundle mBundle = new Bundle();  
			mBundle.putParcelableArrayList("User_Arry", mArrayListUsers);   //传递一个user对象列表
		    intent.putExtras(mBundle);  	
		    setResult(RESULT_OK, intent);
			finish();
		}
		return true;
	}

}

IntentSerializableActivity.class

package com.example.intentdemo;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.TextView;

public class IntentSerializableActivity extends Activity {
	private ArrayList<Student> mArrayListStudent=new ArrayList<Student>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		TextView mTextView = new TextView(this);

		Student student = new Student();
		Bundle bundle = this.getIntent().getExtras();
		if (bundle != null) {
			student = (Student) bundle.getSerializable("student_key");
			mArrayListStudent = (ArrayList<Student>) bundle.getSerializable("student_Arry");
		}

		mTextView.append("You name is: " + student.getName() + "\n"
				+ "You age is: " + student.getEage() + "\n" + "\n");

		for (int i = 0; i < mArrayListStudent.size(); i++) {
			Student studentArrayList = new Student();
			studentArrayList = mArrayListStudent.get(i);
			mTextView.append("You name is: " + studentArrayList.getName() + "\n"
					+ "You age is: " + studentArrayList.getEage() + "\n");
		}

		setContentView(mTextView);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	// 返回按键调用
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent();
			Bundle mBundle = new Bundle();  
		    mBundle.putSerializable("student_Arry", mArrayListStudent);   //传递一个user对象列表
		    intent.putExtras(mBundle);  	
		    setResult(RESULT_OK, intent);
			finish();
		}
		return true;
	}

}

xml布局我就不贴了,需要源码可以下载。

其实看一下代码,都是能看明白。也很容易实践,用过一遍之后就好很多了。

下载地址:点击此处下载


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。