0

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?

Abdul Ali
  • 27
  • 5
  • I believe the problem is that you're creating an instance of the fragment yourself. That's not supposed to happen. The fragment should be constructed by the system unless you're using the fragment manager - which doesn't seem to be the case here. Try passing data using the `putExtra` methods on the Intent. – Milack27 Sep 07 '20 at 23:56

3 Answers3

0

Here is a simple way to serialize the object, store them in the SharedPreferences and then in the fragment you read the object.

In you Gradle: implementation 'com.google.code.gson:gson:2.8.5'

In your Activity:

  val prefsEditor = getSharedPreferences()!!.edit()
  val gson = Gson()
  val json = gson.toJson(institucion)
  prefsEditor.putString("institucion", json)
  prefsEditor.commit()

In the fragment you can read this way

  val preferences = (activity as YourActivity).getSharedPreferences()
  val gson = Gson()
  val json = preferences.getString("institucion", "")
  ong = gson.fromJson(json, Institucion::class.java)

This is in kotlin, but in java is the same concept.

Martin Bove
  • 645
  • 6
  • 11
0

From the code on how you're calling your fragment from your activity, you're not passing in the fragment with the setArguments(); You're only passing an intent from your activity to your fragment. For you to open your fragment from your activity with the bundle, you should check the answers here How to start Fragment from an Activity.

You can also create a new Instance in your fragment to pass in the bundle, and then create a constant to store the value.

public static AktifFragment newInstance(List<Items> impianItems) {
   Bundle bundle = new Bundle();
   AktifFragment aktifFragment = new AktifFragment();
   bundle.putSerializable("Data",impianItems);
   aktifFragment.setArguments(bundle);
   return aktifFragment;
}

And then in your onCreate, you get it like this

items = (ArrayList<ImpianItem>) getArguments().getSerializable("Data");
e.unix
  • 96
  • 6
0

You cannot use startActivity to 'launch a Fragment'.

Instead of a fragment, you can use an Activity (AktifActivity in your case) and pass the items using extras like the following:

Intent intent = new Intent(AddImpianActivity.this, AktifActivity.class);
bundle.putSerializable("Data", impianItems);
startActivity(intent);

In the AktifActivity you will be able to get the items using the following:

private ArrayList<ImpianItem> impianItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_aktif);
    
    Intent intent = getIntent();
    impianItems = (ArrayList<ImpianItem>) intent.getSerializable("Data");

    ...
}

However, if you want to use a fragment, then instead of using startActivity, you should have a FrameLayout in your activity's xml file with an specific id, for example:

<FrameLayout
    android:id="@+id/my_fragment"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

And then in your activity code, use a fragment manager to place the fragment in that FrameLayout:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
AktifFragment fragment = new FooFragment();
Bundle args = new Bundle();
args.putParcelable("Data", impianItems)
fragment.setArguments(args)
ft.replace(R.id.my_fragment, fragment);
ft.commit();
fernandospr
  • 2,976
  • 2
  • 22
  • 43
  • I'm input data in the`activity` class, not from `fragment` and should use the `getSerializable` method is `Fragment` class, but I am confused about how to put the `getSerializable` method, whether in the main `fragment` class or in `adapter` list class? – Abdul Ali Sep 08 '20 at 03:32