I have and Intent and I pass, from the MainActivity, two ArrayLists of Parcelables to a “FormActivity”, that receives the ArrayLists perfeclty (using startActivityForResult), returns perfectly the data gathered throught the form.
The problem is that, if I click the button to open again the FormActivity, the two ArrayLists are not sent again, and I get a NullPointerException.
Have anyone ever seen this?
Thank you.
MainActivity code:
public void insertProductButtonClicked(View view){
Intent addProductIntent = new Intent(getBaseContext(), AddProductActivity.class);
addProductIntent.removeExtra("consumers");
addProductIntent.removeExtra("products");
addProductIntent.putParcelableArrayListExtra("consumers", consumers);
addProductIntent.putParcelableArrayListExtra("products", products);
startActivityForResult(addProductIntent, MyActivities.ACTIVITY_ADD_PRODUCT);
}
AddProductActivity code (“FormActivity”):
Intent intent = new Intent();
intent.putExtra("productName", productNameEditText.getText().toString());
intent.putExtra("productPrice", Double.valueOf(productPriceEditText.getText().toString()));
SparseBooleanArray checkedItems = consumersListView.getCheckedItemPositions();
ArrayList<Integer> consumersToAdd = new ArrayList<Integer>();
for (int i = 0; i < consumers.size(); i++) {
if (checkedItems.get(i)){
consumersToAdd.add(consumers.get(i).getId());
}
}
intent.putIntegerArrayListExtra("productConsumers", consumersToAdd);
setResult(Activity.RESULT_OK, intent);
finish();
You can declare your ArrayList as a static, check below…
By doing this you can access your ArrayList from anywhere by type
where activity_name is the activity or class in which you declare the static ArrayList.
Good luck.