Okay so I have a fragment inside an activity. I’m trying to pass an int from the intent that launched the activity to this fragment, but for some reason I’m getting 0 from the fragment’s arguments. What am I doing wrong?
This is the code responsible for passing the int over:
public void initActivity() {
toolbar = findViewById(R.id.toolbar);
resName = getIntent().getExtras().getString("Name");
int ResID = getIntent().getExtras().getInt("ResID");
Log.d("SplitBill","From initActivity(): We have received ResID as " + ResID);
Bundle bundle = new Bundle();
bundle.putInt("ResID", ResID);
Log.d("SplitBill",bundle.getInt("ResID") + " has been put into the bundle.");
PlaceholderFragment pf = new PlaceholderFragment();
pf.setArguments(bundle);
toolbar.setTitle(resName);
}
This is the code responsible for receiving the int:
Activity activity = getActivity();
Bundle args = getArguments();
MenuViewModel viewModel = ViewModelProviders.of(this, new MenuViewModelFactory(activity.getApplication(), args.getInt(“ResID”))).get(MenuViewModel.class);
The code works just fine if I manually enter a number into where args.getInt() is above, I’m just struggling to get the int from the arguments that I set above.
Sorry for bad formatting, I typed this on my phone.
ResID declaration in MenuActivity: private int ResID;