Skip to content Skip to sidebar Skip to footer

Passing Data From Activity To Fragment Transaction.replace()

I have a problem when passing data from activity to fragment(fragments are overlapped): I need to pass arraylist with integer that contains id of checkboxes to checked... Before pa

Solution 1:

in MyListFragment

private ArrayList mArrayList;

public void setArrayList(ArrayList arraylist)
{
    this.mArrayList = arraylist;
}

you can access mArrayList anywhere

then do

    Bundle bundle = new Bundle();
    bundle.putIntegerArrayList("oki", hm);
    System.out.println("PERO:" + bundle);

    MyListFragment myFragment = new MyListFragment();
    myFragment.setArrayList(hm);


    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.container, myFragment);


    transaction.commit();

Solution 2:

In your Activity

     private List<Data> List = new ArrayList<>();        

     FragmentName FragName = new FragmentName();
     Bundle bundle = new Bundle();                              
     bundle.putParcelable("key",List);
     FragName.setArguments(addEditBundle);
    
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
     transaction.replace(R.id.container, FragName);
    
     transaction.commit();

In your Fragment

     private List<Data> List = new ArrayList<>();  
     List  = getArguments().getParcelable("Key");

Post a Comment for "Passing Data From Activity To Fragment Transaction.replace()"