1

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;

Siku M.
  • 99
  • 1
  • 9

2 Answers2

0

Sender: Activity class inside the @Override onCreate().

Receiver: Fragment class inside the @Override onCreateView().

Android lifecycle diagram for Activity and Fragment.

Android documentation for Bundle.

public class MyApplication extends AppCompatActivity {
 @Override onCreate(...){
  Bundle bundle = new Bundle();
  bundle.putInteger("myint", YOUR_INT_GOES_HERE);
  this.setArguments(bundle);

  //see your int and make sure it's inside the bundle.
  Log.d("onCreate()", "my sender : " + YOUR_INT_GOES_HERE 
  + " what i'm sending: " + bundle.getString("myint") );
 }
}
public class MyFragmentExtension extends Fragment {
 @Override onCreateView(...){
  int receiver = getArguments().getString("myint");
  Log.d("onCreateView()", "my receiver: " + receiver);
 }
}
EvOlaNdLuPiZ
  • 600
  • 1
  • 4
  • 21
  • Okay so I’ve moved the sender code to onCreate() and the receiver code was always inside onCreateView(). ResID is still being passed as 0 inside the fragment – Siku M. Jul 17 '18 at 16:11
  • what is it being initialized as? can you update the code where you initialize ResId and perhaps also where you make an update that is non-zero. – EvOlaNdLuPiZ Jul 17 '18 at 16:52
  • I've updated with ResID's declaration. It is received from a RecyclerViewAdapter from another activity where the views all have identifiers retrieved from a database, none of which are 0. (I checked the database myself using the `sqlite3` binary on my phone, the identifiers start at 1) – Siku M. Jul 17 '18 at 17:08
  • I've tried your addition. The onCreate() code works, but still passes 0 to the Fragment. – Siku M. Jul 17 '18 at 18:24
  • i'm curious about something, change int resId = 12345; pass that and test it. – EvOlaNdLuPiZ Jul 17 '18 at 18:26
  • Done, 12345 is confirmed inside the bundle but the Fragment still returns 0. – Siku M. Jul 17 '18 at 18:31
  • Okay, let's try one more. After `this.setArgument(bundle)` add the following after, `MyFragmentExtension temp = new MyFragmentExtension ();` which creates a class reference and then call `temp.setArguments(bundle);` – EvOlaNdLuPiZ Jul 17 '18 at 18:36
  • Okay so I’ve done MyFragmentExtension pf = new MyFragmentExtension(); and pf.setArguments(bundle) and that still returns 0 – Siku M. Jul 17 '18 at 18:39
  • this seems odd. is there a git repo you can setup for those interested, such as myself, to reproduce the issue? – EvOlaNdLuPiZ Jul 17 '18 at 18:53
  • it's a wild shot, but try File > Invalidate Caches/Restart, then run the program one more time. See if it's not some cache causing this issue. – EvOlaNdLuPiZ Jul 17 '18 at 18:54
  • I could zip the entire project up and DM you the link, then tell you the problem files. – Siku M. Jul 17 '18 at 18:54
  • I'm thinking for the community here , so others can reproduce as well, but sure, i don't mind that either. proceed. – EvOlaNdLuPiZ Jul 17 '18 at 18:56
  • @SikuM. if this solved your problem please vote the answer. thanks! – EvOlaNdLuPiZ Jul 23 '18 at 21:05
0

Fixed it! Since passing the int down in the Bundle didn't work at all (see @EvOlaNdLuPiZ answer and comment thread), I declared it as a public static int at the top of the class, and accessed it using MenuActivity.ResID in the fragment's onCreateView() method. Thanks for all your help!

Siku M.
  • 99
  • 1
  • 9
  • That won't work across process death: https://stackoverflow.com/questions/49046773/singleton-object-becomes-null-after-app-is-resumed/49107399#49107399 – EpicPandaForce Jul 21 '18 at 21:58
  • I know it won't. This activity cannot be opened any other way than through the activity that calls it (unless you use `am`, then the app will just crash), and cannot be navigated back to, that's why I took this approach. – Siku M. Jul 21 '18 at 22:04
  • Every activity can be started by the system, your `ResID` will be initialized to `0` and you'll most likely crash. Well, unless your Fragment is only ever used in MenuActivity, in which it'll coincidentally work. Based on your code, the problem is that `pf` was never actually added to the FragmentManager, so you were probably adding the fragment elsewhere *without* the arguments. – EpicPandaForce Jul 21 '18 at 22:06
  • Also @EpicPandaForce I said that this activity cannot be opened any other way because it has to be launched from the main activity, which passes information down. If this information doesn't exist, the app is going to crash with a `java.lang.NullPointerException`. Just thought I'd clarify. – Siku M. Jul 22 '18 at 14:54