When trying to exchange data between fragments or when passing from activity to a fragment, getArguments always returns null . The reason I personally do not understand.

Activation code:

 FragmentManager fragmentManager; FragmentTransaction fragmentTransaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentManager = getSupportFragmentManager(); } public void OnClick(View view) { fragmentTransaction = fragmentManager.beginTransaction(); switch (view.getId()) { case R.id.button: { fragmentTransaction.add(R.id.container,new fragment2()); new fragment2().newInstance(10); break; } } fragmentTransaction.addToBackStack(null); fragmentTransaction.commit(); } 

Fragment code:

  public class fragment2 extends Fragment implements View.OnClickListener { public static fragment2 newInstance(int columnCount) { fragment2 fragment = new fragment2(); Bundle args = new Bundle(); args.putInt("ARG_COLUMN_COUNT", columnCount); fragment.setArguments(args); return fragment; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View viewFragment = inflater.inflate(R.layout.fragment2, container, false); if (getArguments() != null) { int a = getArguments().getInt("ARG_COLUMN_COUNT"); } else { Toast.makeText(getActivity(), "No getArguments", Toast.LENGTH_SHORT).show(); } return viewFragment; } 

Accordingly, it always returns getArguments() == null . Tell me why is he coming back? I did not find a clear solution anywhere. Thank.

  • With this code, your fragment2 should not appear anywhere on the idea. The names of classes in Java are usually capitalized, like small things, and reading your code is very difficult if you publish code, respect other programmers and stick to the Java convention. - pavlofff
  • This is a test code, so the names are. And why the fragment should not be displayed anywhere? - Kamenev_D
  • Not working anyway. These were tails from previous attempts. Therefore, I picked up that would not confuse anyone. From this point on, please. Not understood. - Kamenev_D pm
  • If correctly understood, then two instances are created. And one of them just returns null. How to pack everything in one? Understood :) fragment2 frag2 = new fragment2 (); fragmentTransaction.add (R.id.container, frag2.newInstance (10)); - Kamenev_D
  • Incorrectly, you all understood that the instance is created by the instance and it is not necessary to create it before invoking the instance. See my answer . Just read what you need to do when someone answered your question , the same goes for your other questions, did not one of the answers to them solve your problem? - pavlofff

1 answer 1

You attach one instance of the fragment2 class to the markup without invoking the instance and, accordingly, without calling setArguments() . Another instance, with a call to an instance, just hangs "in the air" without being displayed anywhere.

Call the instance instance so:

 fragment2 fragment = fragment2.newInstance(10); fragmentTransaction.add(R.id.container, fragment); 

Remove this line altogether:

 new fragment2().newInstance(10); 

See this official guide on working with fragments, where, among other things, there is a way with passing arguments through the instance .

  • Thank you so much! You helped me! - Kamenev_D