Passing object array list between fragments in Android development

android, android-fragments, java

Solution

ArrayList<Transaction> transactionList = new ArrayList<>();

pass the transactionList to the bundle

Bundle bundle = new Bundle();
bundle.putSerializable("key", transactionList);

and in the receiving fragment

ArrayList<Transaction> transactionList = (ArrayList<Transaction>)getArguments().getSerializable("key");

NOTE: to pass your bean class via bundle you have to implement serializable i.e

YourBeanClass implements Serializable

Problem

I am trying to pass arraylist between fragments in Android development. This is the part where I tried to pass Transaction array list to another fragment: ``` switch (menuItem.getItemId()){ case R.id.expenses: final ExpenseActivity expenseFragment = new ExpenseActivity(); new GetAllTransactionAsyncTask( new GetAllTransactionAsyncTask.OnRoutineFinished() { public void onFinish() { FragmentTransaction expsenseTransaction = getSupportFragmentManager().beginTransaction(); Bundle bundle = new Bundle(); //bundle.putParcelableArrayList("transactionlist", GetAllTransactionAsyncTask.allTransaction); //bundle.putString("transactionlist", GetAllTransactionAsyncTask.allTransaction); expenseFragment.setArguments(bundle); expsenseTransaction.replace(R.id.frame,expenseFragment); expsenseTransaction.commit(); } }).execute(session_accountID); return true; } ``` The `GetAllTransactionAsyncTask.allTransaction`will return a Transaction array list. As for my transaction entity class, I implemented Serializable: ``` import java.io.Serializable; @SuppressWarnings("serial") public class Transaction implements Serializable{ ... } ``` I not sure how do I actually pass an object array list between fragments. I commented out the two lines as they are incompatible type. Any ideas? Thanks in advance.

Original source