Hi I have an activity A with a button that sends to Activity B with ActionBar and up button enabled, this activity has tabs with fragments and a button that send to a new fragment C not related with the tabs. By clicking the back button, it is supposed to return to the tabs. The back button works and shows the tabs but not the contain. What I'm doing wrong? Here's some code. Thanks in advance. Activity B
public class BackActivity extends AppCompatActivity {
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_back);
// get fragment manager
myChildToolbar =(Toolbar)findViewById(R.id.my_child_toolbar2);
setSupportActionBar(myChildToolbar);
// Get a support ActionBar corresponding to this toolbar
ActionBar ab = getSupportActionBar();
// Enable the Up button
ab.setDisplayHomeAsUpEnabled(true);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
myChildToolbar.setTitle(R.string.menu_ver_lista);
Bundle bundle=new Bundle();
bundle.putInt(NuevoinformeFragment.INFORMESEL,datosRecuperados.getInt(NuevoinformeFragment.INFORMESEL));
bundle.putString(TabsFragment.ARG_MUESTRA,"true");
TabsFragment detailFragment = new TabsFragment();
detailFragment.setArguments(bundle);
ft.add(R.id.back_fragment, detailFragment);
ft.commit();
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return false;
}
}
Tabs fragment
TabLayout tabs;
ViewPager viewPager;
private ListaDetalleViewModel mViewModel;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_lista_tabs, container, false);
mViewModel = new ViewModelProvider(requireActivity()).get(ListaDetalleViewModel.class);
return root;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
Bundle bundle = getArguments();
viewPager = view.findViewById(R.id.view_pager);
tabs = view.findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
mViewModel.cargarPestañas(ciudadNombre).observe(getViewLifecycleOwner(), words -> {
configureTabLayout();
});
}
private void configureTabLayout() {
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter( getFragmentManager(),clientes, mViewModel,clientesplan);
viewPager.setAdapter(sectionsPagerAdapter);
viewPager.addOnPageChangeListener(new
TabLayout.TabLayoutOnPageChangeListener(tabs));
tabs.addOnTabSelectedListener(new
TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
}
You're using the wrong FragmentManager
when creating your SectionsPagerAdapter
, you should be using getChildFragmentManager()
here:
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(
getFragmentManager(), // This is the problem
clientes, mViewModel,clientesplan
);
getFragmentManager()
actually refers to the FragmentManager
that your TabsFragment
has been added to - that's why that method was deprecated and replaced with getParentFragmentManager()
, which is more explicit on what that FragmentManager
means.
Whenever the fragment you create is fully contained in the layout of another fragment, it needs to be a child fragment by being added to the getChildFragmentManager()
. This ensures that it moves through lifecycle states properly (such as getting its Views recreated when its parent has its Views recreated) and to ensure that the state of the fragments are saved and restored properly.