2021年1月2日星期六

getArguments() always returns null in Fragment

I need to pass arguments to my fragments but getArguments() alway returns null

    public static PersonFragment newInstance(int columnCount, ArrayList<Person> personenListe) {      PersonFragment personFragment = new PersonFragment();      Bundle args = new Bundle();      args.putSerializable("persList",personenListe);      args.putInt(ARG_COLUMN_COUNT, columnCount);      personFragment.setArguments(args);      return new PersonFragment();  }  @Override  public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);        if (getArguments() != null) {                              //getAguments() == null !!          mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT);          mPersonenListe = (ArrayList<Person>) getArguments().getSerializable("persList");      }  }  

I'm calling it in MainActivity

openFragment(PersonFragment.newInstance(personenListe.size(), personenListe));  

With this method

public void openFragment(Fragment fragment) {      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();      transaction.replace(R.id.container, fragment);      transaction.addToBackStack(null);      transaction.commit();  }  
https://stackoverflow.com/questions/65545655/getarguments-always-returns-null-in-fragment January 03, 2021 at 08:50AM

没有评论:

发表评论