Bundle在活动之间失去了它的价值

问题描述 投票:0回答:1

我使用以下代码来启动bundle extras数据的活动,我可以看到数据已保存在Intent extras中:

public static Intent newIntent(Context packageContext, AccountItem account, TransactionItem transaction) {
    Bundle args = new Bundle();
    args.putParcelable("arg_key_account", account);
    args.putParcelable("arg_key_transaction", transaction);
    args.putInt("test_key", 18);

    Intent intent = new Intent(packageContext, TransactionConnectActivity.class);
    intent.putExtra("arg_key", args);
    intent.putExtra("test_key", 21);
    return intent;
}

enter image description here

但是,当我尝试从intent获取bundle extras数据时,bundle没有数据:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    ...
    Intent intent = getIntent();
    if (intent != null) {
        int test = intent.getIntExtra("test_key", -1);
        Bundle args = intent.getExtras().getBundle("arg_key");
    }
    ...
}

enter image description here

自定义对象已实现Parcelable,并且所有成员都已写入parcel。我已经阅读了这些问题和答案,但仍然无法弄清楚我错在哪里?

android android-intent parcelable android-bundle
1个回答
1
投票

为了确保Parcelable工作,应该使用正确的方法writeToParcelcreateFromParcel,否则Android无法正确检索数据,这将导致createFromParcel返回一个空对象。

至于这个问题,我使用writeValue来包裹一个String成员,它必须使用writeString

© www.soinside.com 2019 - 2024. All rights reserved.