October 26, 2024
Chicago 12, Melborne City, USA
Android

Creation inner fragments in onCreateView of another fragment


I have a fragment containing a table (scrollable list). Each row of this table is a fragment too (subfragment). All these subfragments are created in the onCreateView method of the root fragment and these subfragments do not have default (without arguments) constructors. I supposed that they do not need default constructors because there are no layouts with these subfragments and Android must call firstly the onCreatView method and they all will be recreated. But it appeared that sometimes Android tries to call default constructors for them.

I can create for them default constructors and provide them with underlaying global data structures. But if Android creates all these subfragments by itself and then it calls the onCreateView of the root fragment it may results in having two copies of all subfragments in the list! So I have some questons:

  1. Is it a bad practice to create some views or fragments in onCreateView and I am allowed only to call findViewById here? Should I do this in onCreate or in the root fragment constructor?
  2. Does Android call onCreate or onCreateView in the situation when it recreates the page this way?
    Thanks for any answer!



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video