1

I am trying to pass an arraylist from one activity to another activity's fragment. I searched a lot for the solution but couldn't get a working answer.

This is where I am passing the arraylist.

    Intent intent = new Intent(SizeActivity.this,CartActivity.class);
                intent.putExtra("PrintList", photoPrintsList);
                startActivity(intent);

Accepting in the activity with the fragment.

public class ExampleActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);

    if (savedInstanceState == null) {
        Fragment newFragment = new CartActivityFragment();
        newFragment.setArguments(getIntent().getExtras());
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        ft.commit();
    }
}

And this is the fragment. How do I pass data from one activity to another fragment.

 private ArrayList<PhotoPrints> printsArrayList = new ArrayList<>();
private String TAG = "CartActivityFragment";


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_cart, container, false);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

    Bundle b =  getArguments();
    if(b != null) {
        Log.d(TAG,b.size() + "");
        printsArrayList = b.getParcelableArrayList("PrintList");
        Log.d(TAG,"Got arraylist");
    } else {
        Log.d(TAG,"Null");
    }

}

EDIT:

I found that my onCreateView of the fragment is being called before the onCreate of the holding activity. This way I am not able to relay data from the activity to the fragment. Have a look at the Log.

09-21 17:21:32.539  22672-22672/in.reduxpress   D/CartActivityFragment﹕ Null checker
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivityFragment﹕ Null
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ Received Arraylist
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0014.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 0 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0015.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 1 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150915-WA0003.jpg
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 4 X 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 Glossy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 Economy
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ 2 6
09-21 17:21:32.539  22672-22672/in.reduxpress D/CartActivity﹕ Size of bundle 1

How do I bypass this behaviour?

Edit2: I have tested extensively. I am able to receive the data in the activity successfully. I put it in a bundle and set the bundle as argument. But I am not able to receive the bundle in the fragment. It is always giving me a null value.

I have tried putting getArguments in onCreateView as well as onActivityCreated, both don't work. Please help.

0 Answers0