Parcelable(SDK)
Interface for classes whose instances can be written to and restored from aParcel
.
Classes implementing the Parcelable interface must also have a static field calledCREATOR
, which is an object implementing theParcelable.Creator
interface.
Passing data between activities is quite easy.
You would normally do that using the Bundle packed into an intent. But sometimes you need to pass complex objects from one activity to another.
One workaround would be to keep a static instance of the object int your Activity and access it from you new Activity. This might help, but it's definitely not a good way to do this.
To pass such objects directly through the Bundle, your class would need to implement the Parcelable interface.
Example:
MainActivity, SubActivity, Person
MainActivity
package com.learn;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "Parcelable";
public static final String KEY = "key";
private Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "MainActivity");
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
if(R.id.btn == v.getId()){
Person mPerson = new Person();
mPerson.setName("Bill");
mPerson.setAge(22);
Bundle bundle = new Bundle();
bundle.putParcelable(KEY, mPerson); // mPerson -> Parcelable
Log.d(TAG, "mPerson=" + mPerson);
Intent intent = new Intent(this, SubActivity.class);
intent.putExtras(bundle);
startActivity(intent);
Log.d(TAG, "startActivity");
}
}
}
SubActivity
package com.learn;
import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.widget.TextView;
public class SubActivity extends Activity {
private static final String TAG = "Parcelable";
private TextView tv;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d(TAG, "SubActivity");
Parcelable parcelable = getIntent().getParcelableExtra(MainActivity.KEY);
Person mPerson = (Person)parcelable; // Parcelable -> Person
Log.d(TAG, "parcelable=" + parcelable + "; mPerson=" + mPerson);
tv = (TextView)findViewById(R.id.tv);
tv.setText("name=" + mPerson.getName() + "; age=" + mPerson.getAge());
}
}
Person
package com.learn;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;
public class Person implements Parcelable{
private static final String TAG = "Parcelable";
private String name;
private int age;
public Person(){
}
public void setName(String name){
this.name = name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return this.age;
}
public static final Parcelable.Creator<Person> CREATOR = new Creator<Person>() {
@Override
public Person createFromParcel(Parcel source) {
Log.d(TAG, "createFromParcel(Parcel source)");
Person mPerson = new Person();
mPerson.name = source.readString();
mPerson.age = source.readInt();
return mPerson;
}
@Override
public Person[] newArray(int size) {
Log.d(TAG, "newArray(int size)");
return new Person[size];
}
};
@Override
public int describeContents() {
Log.d(TAG, "describeContents()");
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
Log.d(TAG, "writeToParcel(Parcel dest, int flags)");
dest.writeString(name);
dest.writeInt(age);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Parcelable" />
</LinearLayout>
Running Result
click, then
Log
D/Parcelable( 487): MainActivity
D/Parcelable( 487): onClick
D/Parcelable( 487): mPerson=com.learn.Person@40520b98
D/Parcelable( 487): writeToParcel(Parcel dest, int flags)
D/Parcelable( 487): startActivity
D/Parcelable( 487): SubActivity
D/Parcelable( 487): createFromParcel(Parcel source)
D/Parcelable( 487): parcelable=com.learn.Person@405269f8; mPerson=com.learn.Person@405269f8
Analyse
writeToParcel(Parcel dest, int flags) ---- bundle.putParcelable(KEY, mPerson)
createFromParcel(Parcel source) ---- (Person)getIntent().getParcelableExtra(MainActivity.KEY)
source ---- dest
Download
Parcelable - How to do that in Android
Writing Parcelable classes for Android
分享到:
相关推荐
在Android studio 4.0+以后在插件中已经搜索不到Android Parcelable code generator这个超级好用的插件了,唯一能使用的方式就是下载离线插件包,进行离线安装。 File -> Settings -> Pugins -> Install plugin from...
在Android studio 4.0+以后在插件中已经搜索不到Android Parcelable code generator这个超级好用的插件了,唯一能使用的方式就是下载离线插件包,进行离线安装。
android-parcelable-intellij-plugin, 面向 Android Parcelable样板代码生成的IntelliJ插件 intellij/Android Studio 插件,用于 Android Parcelable代码生成这里工具基于类中的字段生成一个 Android Parcelable插件...
在Android中用Parcelable序列化虽然效率高,但是比Serializable麻烦很多;现在只要添加一个Jar包就可以省去序列化的过程,很方便。 Jar包在项目里,可以直接使用,也可以查看里面的文本文件"README.md
该工具根据类中的字段生成一个Android Parcelable实现。该工具是同名原始插件的新版本 用法:在编辑器中按ALT +插入(或用于代码生成的等效键绑定),然后选择Parcelable。它允许您选择要打包的字段。
android序列化(Parcelable)
在Android开发过程中,经常要在Activity之间传递参数,使用Android系统提供的方法可以传递基本数据类型的变量,但有时候我们经常要传递一些复杂的数据类型或自定义的类,这种情况的参数无法直接传递,我们可以通过...
Android Parcelable接口使用方法详解 1. Parcelable接口 Interface for classes whose instances can be written to and restored from a Parcel。 Classes implementing the Parcelable interface must also have ...
AndroidStudio自动生成parcelable的插件
老插件已经不可用,改插件支持Android studio 4.1版本
android parcelable code generator
Android通过Parcelable和Serializable实现各种类型嵌套序列化
parcelable android demo,自己编写测试通过
NULL 博文链接:https://2528.iteye.com/blog/1849692
主要介绍了Android Parcelable与Serializable详解及区别的相关资料,需要的朋友可以参考下
Android序列化——Serializable与Parcelable
Android Parcelable 实现 - 基于 SQL 表的数据库字段名称常量。 新字段与先前添加的字段按顺序添加。 因此,如果字段已添加并以相同方式定义,则将跳过它。 CreateTable 字段与 SQL 创建表语句。 完整投影字段 -...