Passing Data from activity to fragment Android

While developing my application I came across a situation in which i had six fragments four on the same activity and two fragment on one fragment and each fragment is loading some data from the internet sending the Volley Request. It was creating there Volley Request in the same time. Ideally it should send only one. So, I wrote a code in which there is only one single request to server in the beginning.
Next thing is to send the data in the form of list to the fragments. For that we have to go there where the object of the fragment is created and using that object we have to pass the data to the fragment in the form of arguments and arguments accept only Bundle, so we have to put our list in Bundle first.
Important thing is that the bean or pojo of which our list is a type should be implementing Serializable.
The code is like

Bundle bundle= new Bundle();
FragmentTransaction fragmentTransactionHome=getSupportFragmentManager().beginTransaction();
UserHomeFragment userHomeFragment = new UserHomeFragment();
bundle.putInt("userPoints",totalPoints);
bundle.putSerializable("dealsArrayList",dealsArrayList);
userHomeFragment.setArguments(bundle);
fragmentTransactionHome.add(frameLayout, userHomeFragment);
fragmentTransactionHome.commit();




Comments

Popular posts from this blog

Styling Bottom Navigation

Lottie Animation in Android

Set Up the AWS Mobile SDK for Android