在活动之间传递自定义对象?

12

我如何在Android的活动之间传递自定义对象?我知道有捆绑包(bundles)但是却无法在其中找到相关功能。是否能有人给我展示一个不错的例子?

5个回答

16

3
为什么不能在 Bundle 中发送对象?我的意思是,为什么 Android 不支持它?所有的活动都在同一个进程中运行,所以支持它不是很容易吗? - Sudarshan Bhat
2
@Enigma,我想我明白你的问题了。如果你在bundle中指的是“传递对象引用”,那么这是一个不好的主意,因为创建对象的Activity可能会因为内存不足而被销毁,从而导致对象丢失。 - user468311
@EhsanMirsaeedi 如果你的引用阻止了封闭对象被垃圾回收,那么你就会出现内存泄漏。 - user468311
@Igor Filippov 链接已失效。 - Sven van den Boogaart
@SvenB 我想安卓开发者文档已经解释得足够好了。 - user468311
显示剩余2条评论

5
使用Parcelable接口,您可以将自定义Java对象传递到意图中。
1)将Parcelable接口实现到您的类中,如下所示:
class Employee implements Parcelable
{
}

2) 将Parcelable对象传递到意图中,如下所示:

Employee mEmployee =new Employee();
Intent mIntent = new Intent(mContect,Abc.class);
mIntent.putExtra("employee", mEmployee);
startActivity(mIntent);

3) 将数据传递到新的 [Abc] Activity 中,如下所示:

Intent mIntent  = getIntent();
Employee mEmployee  = (Employee )mIntent.getParcelableExtra("employee");

1

Parcel可能解决您的问题。

Parcel视为原始类型(long、String、Double、int等)的“数组”(比喻)。如果您的自定义类仅由原始类型组成,则更改类声明,包括implements Parcelable

您可以毫不费力地通过意图传递可包装对象(就像发送基本类型对象一样)。在这种情况下,我有一个名为FarmData的可包装自定义类(由长整型、字符串和双精度浮点数组成),我通过意图从一个活动传递到另一个活动。

    FarmData farmData = new FarmData();
// code that populates farmData - etc etc etc
    Intent intent00 = new Intent(getApplicationContext(), com.example.yourpackage.yourclass.class);
    intent00.putExtra("farmData",farmData);
    startActivity(intent00);    

但是检索它可能会有些棘手。接收意图的活动将检查是否随意图一起发送了一组额外的数据。

    Bundle extras = getIntent().getExtras();
    FarmData farmData = new FarmData();
    Intent intentIncoming = getIntent();
    if(extras != null) {
        farmData = (FarmData) intentIncoming.getParcelableExtra("farmData");// OK
    }

0

假设有一个实现了整个对象树的可序列化 PasswordState 对象,你可以将该对象传递到另一个活动中:

private void launchManagePassword() {
    Intent i= new Intent(this, ManagePassword.class); // no param constructor
    PasswordState outState= new PasswordState(lengthKey,timeExpire,isValidKey,timeoutType,"",model.getIsHashPassword());
    Bundle b= new Bundle();
    b.putSerializable("jalcomputing.confusetext.PasswordState", outState);
    i.putExtras(b);
    startActivityForResult(i,REQUEST_MANAGE_PASSWORD); // used for callback
}

0

在活动之间传递对象或使对象对整个应用程序通用的一种简单方法是创建一个扩展Application的类。

以下是一个示例:

public class DadosComuns extends Application{

    private String nomeUsuario="";

    public String getNomeUsuario() {
        return nomeUsuario;
    }

    public void setNomeUsuario(String str) {
        nomeUsuario = str;
    }
}

在您的其他所有活动中,您只需要实例化一个名为“DadosComuns”的对象,并将其声明为全局变量。
private DadosComuns dadosComuns;

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


    //dados comuns
    dadosComuns = ((DadosComuns)getApplicationContext());

    dadosComuns.setNomeUsuario("userNameTest"); }

所有你实例化的其他活动,dadosComuns = ((DadosComuns)getApplicationContext()); 你可以访问 getNomeUsuario() == "userNameTest"

在你的AndroidManifest.xml中需要有

<application
        android:name=".DadosComuns"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接