I'm using bundle for data transfer between fragment
, sender part is ok but at receiving, I'm getting null
value at the bundle.
I have tried everything and I don't find any error but still not getting I've Tried every possible solution. Please let me know what I'm making mistake here.
At first fragment, I'm sending object type data and int type data.
FirsrtFragment.java
lay_upcoming.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
eventFragment = new EventFragment();
bundle.putInt("img_id", img_id2);
eventFragment.setArguments(bundle);
getFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up).addToBackStack(null).replace(R.id.fragment_container, new EventFragment()).commit();
}
});
This is My SecondFragment.java
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
intiView();
View v = inflater.inflate(R.layout.event_detail_fragment, container, false);
bundle = getArguments();
if (bundle != null) {
img_id = getArguments().getInt("img_id");
} else {
Toast.makeText(getContext(), "Bundle is Null", Toast.LENGTH_SHORT).show();
}
ButterKnife.bind(this, v);
return v;
}
You need to pass the fragment where you set the bundle. But you are using a new Fragment. Change the code as below
lay_upcoming.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
bundle = new Bundle();
eventFragment = new EventFragment();
bundle.putSerializable("Data", (Serializable) data2);
Log.i("DAta", String.valueOf(img_id2));
Log.i("DAta", String.valueOf(img_id2));
bundle.putInt("img_id", img_id2);
eventFragment.setArguments(bungetFragmentManager().beginTransaction().setCustomAnimations(R.anim.slide_in_up, R.anim.slide_out_up).addToBackStack(null).replace(R.id.fragment_container, eventFragment).commit(); // change new EventFragment() to eventFragment
}
});
You are creating a new fragment instead of the one you already created and passed arguments: replace(R.id.fragment_container, new EventFragment())
.
Just use the eventFragment
you already created: replace(R.id.fragment_container, eventFragment)
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.