如何使用 Intent 将 Custom Class ArrayList 传递给另一个 Activity?

How to pass Custom Class ArrayList to another another Activity using Intent?

我有两个 Activity,我想将自定义模型 Class ArrayList 传递给另一个 Activity,我该如何实现?

Activity A 中,我使用以下代码传递数组:-

ArrayList<DataModel> arrayList = new ArrayList<>();
someBtn.setOnClickListener(v -> {
    Intent intent = new Intent(ActivityA.this, ActivityB.class);
    intent.putExtra("dataList", arrayList);
    startActivity(intent);
}

但我不知道如何使用 intent.Activity B 获取 dataList

请分享解决方案,我怎样才能在 Activity B.

中获取 dataList

在 Activity 内 B 在 onCreate() 内使用方法 getIntent() 并检查它是否有数据

示例方法:

Public boolean getExtraData(Intent intent){
   if(intent.hasExtra("dataList")){
      this.dataList = intent.getParcelableArrayListExtra("dataList");
   }
}

另请参阅:Intent.java

/**
 * Retrieve extended data from the intent.
 *
 * @param name The name of the desired item.
 *
 * @return the value of an item previously added with
 * putParcelableArrayListExtra(), or null if no
 * ArrayList<Parcelable> value was found.
 *
 * @see #putParcelableArrayListExtra(String, ArrayList)
 */
public @Nullable <T extends Parcelable> ArrayList<T> getParcelableArrayListExtra(String name) {
    return mExtras == null ? null : mExtras.<T>getParcelableArrayList(name);
}