0

I'm trying to pass an int value from one activity to Fragment, but the value is not passed and assigned a default value.

The class from which to send the int value

int level=3;

Intent myIntent = new Intent(this, ExampleBottomSheetDialog.class);
myIntent.putExtra("level", level);

The Fragment that should get an int value

int level = getActivity().getIntent().getIntExtra("level", 0);

As I understand it, there should be a completely different code, as we pass the value to the fragment. How to convey the value int from activity to the fragment?

  • Did you debug your code? Are `myIntent` and `getActivity().getIntent()` the same instances? I'm no Android expert but if those aren't the same instances then that's most likely your problem - if they are the same then either your first snippet isn't called or the value is being overwritten somewhere. – Thomas Jul 31 '18 at 10:26
  • Possible duplicate of [Send data from activity to fragment in android](https://stackoverflow.com/questions/12739909/send-data-from-activity-to-fragment-in-android) – Bö macht Blau Jul 31 '18 at 17:36

0 Answers0