user7691120
user7691120

Reputation:

Passing object array list between fragments in Android development

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.allTransactionwill 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.

Upvotes: 4

Views: 16772

Answers (4)

BladeCoder
BladeCoder

Reputation: 12949

Instead of implementing Serializable, you should make your Transaction class implement Parcelable. Parcelable is the fastest serialization mechanism you can use on Android, it's faster than Serializable or JSON transformation and uses less memory.

Upvotes: 0

karthik vishnu kumar
karthik vishnu kumar

Reputation: 1021

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

Upvotes: 18

Arpan Sharma
Arpan Sharma

Reputation: 2162

1.You can either pass it by converting into json as specified by @Bhupat.

2.Another way is you make your Transaction class parcelable and the use

 b.putParcelableArrayList("list",your_list);

for getting list

your_list = getArguments().getParcelableArrayList("list");

EDIT you have too sepcigy type token for getting it back

 transactionlist = new Gson().fromJson(str,  new TypeToken<List<type of the list passed>>);

In your case new TypeToken<List<Transaction>>

Upvotes: 1

Bhupat Bheda
Bhupat Bheda

Reputation: 1978

add this gradle in your build.gradle compile 'com.google.code.gson:gson:2.7'

String str = new Gson().toJson(arrayList);
bundle.putStrin("str",str);

and destination fragemnt

String str = bundle.getString("str");

arrayList = new Gson().fromJson(str,ArrayList.class);

Upvotes: 4

Related Questions