前言:上一章节我们介绍了Intent的两种跳转方式,显式跳转和隐式跳转,今天我们来学习下如何在跳转的过程中进行数据的传递,分别是简单的数据传递、数组传递、集合、对象传递、Bitmap传递!
-------简单的数据传递-----
MainActivity.java:
package com.example.intent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click1(View v) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", "fly");
Bundle bundle = new Bundle();
bundle.putString("name", "fly");
bundle.putString("school", "guet");
intent.putExtra("bundle", bundle);
startActivity(intent);
}
}
SecondActivit.java(注意:Activity要注册):
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getclick1data();
}
private void getclick1data() {
Intent intent = getIntent();
String name = intent.getStringExtra("name");
Bundle bundle = intent.getBundleExtra("bundle");
String bundle_name = (String) bundle.get("name");
String school = (String) bundle.get("school");
System.out.println("----name---->" + name);
System.out.println("----bundle_name---->" + bundle_name);
System.out.println("----school---->" + school);
}
}
运行截图:
------数组传递-----
MainActivity.java核心代码:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", new String[] { "fly", "li" });
Bundle bundle = new Bundle();
bundle.putStringArray("fruit", new String[] { "apple", "banana" });
intent.putExtra("bundle", bundle);
startActivity(intent);
SecondActivity核心代码:
Intent intent = getIntent();
String[] name = intent.getStringArrayExtra("name");
Bundle bundle = intent.getBundleExtra("bundle");
String[] fruit = bundle.getStringArray("fruit");
for (int i = 0; i < name.length; i++) {
System.out.println("----name---->" + name[i]);
}
for (int i = 0; i < fruit.length; i++) {
System.out.println("----fruit---->" + fruit[i]);
}
运行截图:
-----集合传递----
1、List<基本数据类型或者String类型>:
写入:
intent.putStringArrayListExtra(name, value)
intent.putIntegerArrayListExtra(name, value)
读取:
intent.getStringArrayListExtra(name)
intent.getIntegerArrayListExtra(name)
2、List< Object>:
将list强转成Serializable类型,然后传入(可用Bundle做媒介)
写入:
putExtras(key, (Serializable)list)
读取:
(List<Object>) getIntent().getSerializable(key)
3、Map<String, Object>:
//传递复杂些的参数
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("key1", "value1");
map1.put("key2", "value2");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
list.add(map1);
Intent intent = new Intent();
intent.setClass(MainActivity.this,ComplexActivity.class);
Bundle bundle = new Bundle();
//须定义一个list用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundlelist = new ArrayList();
bundlelist.add(list);
bundle.putParcelableArrayList("list",bundlelist);
intent.putExtras(bundle);
startActivity(intent);
------
Intent传递对象----
1)将对象转换为Json字符串
Gson解析的例子:
public class Book{
private int id;
private String title;
//...
}
public class Author{
private int id;
private String name;
//...
}
写入数据:
Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",new Gson().toJson(book));
startActivity(intent);
读取数据:
String bookJson=getIntent().getStringExtra("book");
Book book=new Gson().fromJson(bookJson,Book.class);
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
2)使用Serializable,Parcelable序列化对象
1.Serializable实现:
①业务Bean实现:Serializable接口,写上getter和setter方法
②Intent通过调用putExtra(String name, Serializable value)传入对象实例 当然对象有多个的话多个的话,我们也可以先Bundle.putSerializable(x,x);
③新Activity调用getSerializableExtra()方法获得对象实例: eg:Product pd = (Product) getIntent().getSerializableExtra("Product");
④调用对象get方法获得相应参数
2.Parcelable实现:
一般流程:
①业务Bean继承Parcelable接口,重写writeToParcel方法,将你的对象序列化为一个Parcel对象;
②重写describeContents方法,内容接口描述,默认返回0就可以
③实例化静态内部对象CREATOR实现接口Parcelable.Creator
④同样式通过Intent的putExtra()方法传入对象实例,当然多个对象的话,我们可以先 放到Bundle里Bundle.putParcelable(x,x),再Intent.putExtras()即可
一些解释:
通过writeToParcel将你的对象映射成Parcel对象,再通过createFromParcel将Parcel对象映射 成你的对象。也可以将Parcel看成是一个流,通过writeToParcel把对象写到流里面, 在通过createFromParcel从流里读取对象,只不过这个过程需要你来实现,因此写的 顺序和读的顺序必须一致。
实现Parcelable接口的代码示例:
//Internal Description Interface,You do not need to manage
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags){
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book[] newArray(int size) {
return new Book[size];
}
@Override
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
};
3.两种序列化方式的比较:
两者的比较:
1)在使用内存的时候,Parcelable比Serializable性能高,所以推荐使用Parcelable。
2)Serializable在序列化的时候会产生大量的临时变量,从而引起频繁的GC。
3)Parcelable不能使用在要将数据存储在磁盘上的情况,因为Parcelable不能很好的保证数据的 持续性在外界有变化的情况下。尽管Serializable效率低点,但此时还是建议使用Serializable。
----Bitmap传递-----
bitmap默认实现Parcelable接口,直接传递即可
实现代码:
Bitmap bitmap = null;
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
intent.putExtra("bundle", bundle);
-----完----