how to retrieve the data that we input from the activity
to the fragment
? I tried this with the serializable
method, but I got stuck while receiving data from my activity
and applying it to fragment
Activity class:
Intent intent = new Intent(AddImpianActivity.this, AktifFragment.class);
AktifFragment aktifFragment = new AktifFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("Data",impianItems);
aktifFragment.setArguments(bundle);
startActivity(intent);
This is the complete code for the Activity class: https://codeshare.io/GA4weN
This is code for Activity Item class:https://codeshare.io/aVb1D6
Activity Adapter:https://codeshare.io/aVb1D6
Fragment class:
public class AktifFragment extends Fragment {
ArrayList<ImpianItem> items = new ArrayList<>();
public AktifFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_aktif, container, false);
//null object reference
RecyclerView recyclerView = view.findViewById(R.id.rv_impian_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
items = (ArrayList<ImpianItem>) getArguments().getSerializable("Data");
ImpianAdapter adapter = new ImpianAdapter(items);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(layoutManager);
return view;
}
}
The code above, when I run it and from the message that appears, there is a null error in the serializable
method in the fragment
. Is there a solution to this?