Good day!

My program reads the message and, depending on its content, creates several LinearLayouts with different fillings.

Is it possible to programmatically create a fragment with one of the received LinearLayouts (the number of fragments = the number of layouts and the final number is not known in advance)

    1 answer 1

    Create this method in the snippet:

    public static MyFragment newInstance(int layout) { Bundle args = new Bundle(); args.putInt("layout", layout); MyFragment fragment = new MyFragment(); fragment.setArguments(args); return fragment; } 

    Next, write in onCreateView :

     int layout = getArguments().getInt("layout", 0) View v = inflater.inflate(layout, container, false); return v; 

    call where you need MyFragment fragment = MyFragment.newInstance(my_layout);

    Or, for example, to transfer some number to the fragment, which in the fragment, depending on the value ( switch(getArguments().getInt("number,0)) ), will load the required layout .