Answer the question
In order to leave comments, you need to log in
How to correctly restore a fragment in a fragment?
Good afternoon.
I have not yet encountered the work of a fragment in a fragment, tell me how to restore their state correctly?
There is an activity Activity
FragmentA is installed in it.
FragmentA is set to FragmentB.
In code it looks like this:
// активити
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(R.layout.activity, savedInstanceState);
Fragment fragment = FragmentA.newInstance(....);
setFragment(R.id.container, fragment); // установка с помощью FragmentManager
}
// FragmentA
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View layout = createView(R.layout.fragment_a, inflater);
Fragment fragment = FragmentB.newInstance(...);
setFragment(R.id.container_sub, fragment);
return layout;
}
// в FragmentB ничего какого-то интересного кода нет
// активити
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(R.layout.activity, savedInstanceState);
if (savedInstanceState == null) {
Fragment fragment = FragmentA.newInstance(....);
setFragment(R.id.container, fragment);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View layout = createView(R.layout.fragment_a, inflater);
if (savedInstanceState == null) {
Fragment fragment = FragmentB.newInstance(...);
setFragment(R.id.container_sub, fragment);
}
return layout;
}
Answer the question
In order to leave comments, you need to log in
It's not very clear what the setFragment method is. For this code to work correctly inside a fragment, getChildFragmentManager must be used. Then everything will be ok.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question