How to pass bundle with ArrayList from Fragment to Activity
Did you implement the Parcelable object correctly?
After parceled, you can have different address object because object are recreated with that data.
Check out the Parcelable implementation of the Recipe object.
Passing ArrayList from Fragment class to Activity
Create an custom interface in your Fragment:
public interface OnFragmentInteractionListener {
public void onFragmentSetStudents(ArrayList<Student>);
}
Implement this interface in your Activity:
public class YourActivity extends Activity implements YourFragment.OnFragmentInteractionListener {
private ArrayList<Student> allStudents;
}
Now you have to override the declared method (in your Activity
):
@Override
public void onFragmentSetStudents(ArrayList<Student> students) {
allStudents = students;
}
Then you just have to start this method with an Listener in your Fragment:
OnFragmetInteractionListener listener = (OnFragmentInteractionListener) activity;
listener.onFragmentStudents(allStudents)
How to Pass Custom arraylist from fragment to activity
There are two different ways to send data from activity to fragment.
Pass data as the argument when you create using the factory method.
Through factory method:
public static SimpleFragment getInstance(Bundle bundle) {
SimpleFragment simpleFragment = new SimpleFragment();
simpleFragment.setArguments(bundle)
return simpleFragment;
}
Through this method you can pass simple objects like string.
Still, you can pass some complex objects by converting that object into parseable.
new Bundle().putParcelable("Key", ParcebleObject);
Through the interface
Using interface, you can pass almost any data.
Create an interface, in my case I create an interface called SomeEventListener.
Interface
public interface SomeEventListener {
public onSomeEventHappened(ArrayList<String> arrayListOfString);
}
Fragment class
public class SimpleFragment extends Fragment implement SomeEventHappened {
public interface onSomeEventListener {
private ArrayList<String> arrayList;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment2, null);
//..........
return v;
}
public onSomeEventHappened(ArrayList<String> arrayListOfString) {
this.arrayList = arrayListOfString;
}
}
In your MainActivity.java file
public class MainActivity extends AppCompatActivity {
@override
public void onCreate(Bundle savedInstanceState) {
//........
ArrayList<String> array = new ArrayList<>();
SimpleFragment fragment = new SimpleFragment();
SomeEventListener listener = fragment.getEventListener();
listener.onSomeEventHappened(array);
}
This will work for any kind of object.
How to pass custom object arraylist from activity to fragment with instance
Can you try ?
public static final NewCustomer newInstance(ArrayList<customers> mArrayList) {
NewCustomer f = new NewCustomer();
Bundle bdl = new Bundle(1);
this.data = mArrayList; // assign its Value
bdl.putParcelableArrayList(data, mArrayList);
f.setArguments(bdl);
return f;
}
this.data = mArrayList
will assign value to the current member variable of fragment. Now it can we be accessed in current fragment.
Passing object array list between fragments in Android development
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
Related Topics
Automatically Size Jpanel Inside Jframe
Error: Could Not Find or Load Main Class in Intellij Ide
Spring Webclient: How to Stream Large Byte[] to File
How to Fetch All Links and Click Those Links One by One Using Selenium Webdriver
Failed to Process Import Candidates for Configuration Class
Javafx Column in Tableview Auto Fit Size
How to Mark Cucumber Junit Step as Failed But Continue
Excluding Tests from Being Run in Intellij
I Want to Execute Code Every X Seconds, But Handler.Postdelayed Not Working
Testing Two Json Objects for Equality Ignoring Child Order in Java
How to Check Heap Usage of a Running Jvm from the Command Line
How to Get Full Name of an Employee by Joining First, Middle and Last Name Considering Null in Java
How to Restart a Java Program Here
Recyclerview Not Calling Oncreateviewholder or Onbindview
How to Find the Longest Word in a Given String