天天看點

intent.putExtra() 和 bundle.putExtra()的差別

簡單的在Activity間傳資料,我們一般有兩種方式:

1.直接用Intent的putExtra(), getStringExtra();

2.先new一個Bundle對象,用Bundle的putExtra().

那麼這兩種方式有什麼不一樣呢?

我們先看一下Intent對象相應函數的源代碼:

public Intent putExtra(String name, String value) {
        if (mExtras == null) {
            mExtras = new Bundle();
        }
        mExtras.putString(name, value);
        return this;
    }
           
public String getStringExtra(String name) {
        return mExtras == null ? null : mExtras.getString(name);
    }
           

 可以知道,Intent的相關操作的實作是基于Bundle的,Bundle操作資料相對于Intent有哪些優勢呢?

舉個例子,如果我要從A界面跳轉到B界面和C界面,那麼我要寫寫兩個Intent,如果傳的資料相同,我兩個Intent都要添加,但是如果我用Bundle,直接一次性包裝就可以了。

再有,如果A界面要傳值給B界面,再從B界面傳值到C界面,你可以想象用Intent是多麼的麻煩了,但是用Bundle就很簡潔,而且還可以在B界面中添加新的資訊。

其實在Android代碼中這樣的情況蠻多的,比如Intent的setClass和ComponentName這兩種方式又有什麼差別呢?請看setClass源代碼:

public Intent(Context packageContext, Class<?> cls) {
        mComponent = new ComponentName(packageContext, cls);
    }
           
public Intent setClass(Context packageContext, Class<?> cls) {
        mComponent = new ComponentName(packageContext, cls);
        return this;
    }
           

當然如果傳的資料非常之多而且很複雜,用這兩種方式顯然是不适合的,這時候我們可以使用可序列化的結構類,執行個體代碼如下:

Parcelable類。最主要的類,也就是我們要傳送的對象的類,需要實作Parcelable接口。

package com.zeph.android.Parcelable;

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

public class BenParcelable implements Parcelable {

	public String name;

	public int age;

	public String profession;

	public BenParcelable(String name, int age, String profession) {
		this.name = name;
		this.age = age;
		this.profession = profession;
	}

	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;
	}

	public String getprofession() {
		return profession;
	}

	public void setprofession(String profession) {
		this.profession = profession;
	}

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

	@Override
	public void writeToParcel(Parcel parcel, int flag) {
		parcel.writeString(name);
		parcel.writeInt(age);
		parcel.writeString(profession);
	}

	public static final Creator<BenParcelable> CREATOR = new Creator<BenParcelable>() {
		public BenParcelable createFromParcel(Parcel in) {
			return new BenParcelable(in);
		}

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

	private BenParcelable(Parcel in) {
		name = in.readString();
		age = in.readInt();
		profession = in.readString();
	}
}
           

ParcelableActivity類,傳遞對象的Activity類。

package com.zeph.android.Parcelable;

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

public class ParcelableActivity extends Activity {

	private Button myButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		myButton = (Button) findViewById(R.id.myButton);

		myButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {

				BenParcelable benParcelable = new BenParcelable("BenZeph", 23,
						"Java/Android Engineer");

				Intent intent = new Intent();

				intent.setClass(getApplicationContext(),
						GetParcelableActivity.class);

				Bundle bundle = new Bundle();

				bundle.putParcelable("Ben", benParcelable);

				intent.putExtras(bundle);

				startActivity(intent);

			}
		});
	}
}
           

GetParcelableActivity類,接收序列化對象的Activity類。

package com.zeph.android.Parcelable;

import android.app.Activity;
import android.os.Bundle;

public class GetParcelableActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		BenParcelable parcelable = getIntent().getParcelableExtra("Ben");

		System.out.println(parcelable.getName());

		System.out.println(parcelable.getAge());
		
		System.out.println(parcelable.getprofession());

	}

}