Transferring data between fragments

I want to pass user id from main operation to fragment.

So basically I do:

Fragment fragment = new Fragment(); final Bundle bundle = new Bundle(); bundle.putString("id_User", id); Log.i("BUNDLE", bundle.toString()); fragment.setArguments(bundle); 

And in the magazine I see

 BUNDLE : Bundle[{id_User=1}] 

In the snippet, I test it in onCreate

 Bundle arguments = getArguments(); if (arguments != null) { Log.i("BUNDLE != null","NO NULL"); } else { Log.i("BUNDLE == null","NULL"); } 

And I have

 BUNDLE == null: NULL 

So, the transfer was successful, but how can I get the data in the fragment, please?

+6
source share
2 answers

You can use:

  Bundle args = getArguments(); if (args != null && args.containsKey("id_User")) String userId = args.getString("id_User"); 
+11
source

Just use:

 String User = getArguments().getString("id_User", "Default Value"); 

The default return value will be returned if the key you requested does not exist.

+2
source

Source: https://habr.com/ru/post/958273/


All Articles