Re-inflating different kind in the same fragment in onResume

I have a Fragment

, MainFragment

which can contain two, three or four nested fragments. The specific fragments to be shown can be changed by the user in the settings.

There is a different layout for each number of slices. For example, layout_3

used when the user selects three nested snippets.

What I need to do is dynamically update the layout MainFragment

, and which fragments will be nested in that layout, in onResume()

(i.e. as soon as the user returns from settings). There are about 10 snippets the user can select and I need to be able to dynamically change them in MainFragment

.

I'm having trouble with that. The only way to update the layout / view as soon as I return from settings is to leave MainFragment

and then return (which calls onCreateView()

).

Here is an example of what I'm doing in onCreateView()

to initialize layouts (two nested snippets by default):

mView = mInflater.inflate(R.layout.layout_2, mParent, false);
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer1, fragment1).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer2, fragment2).commit();
return view;

      

Suppose the user goes to settings and selects three nested snippets. This is what I tried in onResume()

, with no effect:

mView = mInflater.inflate(R.layout.layout_3, mParent, false);
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer1, fragment1)).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer2, fragment2).commit();
getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer3, fragment3).commit();

      

I'm not sure if I am doing something wrong. Ideally, I would just like to MainFragment

call again onCreateView()

, but none of the solutions to this problem work.

Any ideas? Thanks for the help.

Edit: I believe the problem is with the bloat of the new View

, not the replacement of the fragments.

For example, suppose the default screen layout_4

has four tile containers. The user then navigates to Settings, which overrides all four default snippets and selects three new snippets. In onResume()

we are trying to inflate layout_3

and then add fragments. I think it layout_3

never bloats, but because my fragment containers have the same styling id

across layouts (i.e. fragmentContainer1

- fragmentContainer4

) the first three fragment containers are updated. The fourth remains as it was as I assumed we were in layout_3

and didn't try to update it.

This is confirmed and crashes when the user tries to increase the number of fragments rather than decrease it. Above, when the user switched from four fragments to three fragments, there was no crash because all three fragment containers I was trying to update exist in layout_4

. But if the user is in layout_2

and then navigates to settings to select the third snippet, we will try to add the snippet to fragmentContainer3

when we resume. It crashes because it layout_3

doesn't bloat.

java.lang.RuntimeException: Unable to resume activity

java.lang.IllegalArgumentException: No view found for id 0x7f0c005f

Any ideas how to fix this? Calling re-inflate mView

in onResume()

doesn't seem to have any effect.

Edit 2: I've tried calling mParent.addView(mView)

after bloat, but still get the same behavior as above for the most part.

+2


source to share


1 answer


When you return from settings, you onResume()

should call in MainFragment

, and then any nested fragments that have already been loaded into MainFragment

. Can't you turn on the update logic in the nested snippets onResume()

and not just in onCreateView()

?

Otherwise, you can create a different code path and put the updating logic: to make public method in a class fragment1

, fragment2

, fragment3

which include all your logic updates, and call these methods somewhere MainFragment

. (You can create an interface and have nested fragment classes inheriting that interface if they are different classes and you want a cleaner design.)



Be careful about whether nested fragments have been resumed, but method calls on View objects when the nested fragments onResume()

have not yet been called can be problematic.

0


source







All Articles