N
N
Neonoviiwolf2018-09-20 08:09:46
Android
Neonoviiwolf, 2018-09-20 08:09:46

Problem with TabLayout, when returning to the previous Fragment, the if (savedInstanceState == null) check does not work?

Good
There is a problem when I scroll through the fragments, then when I return to the first one, for some reason, Bundle is null and the fragments are re-created.
When you rotate the screen, everything
looks like this
5ba32b36aa795828523366.jpeg
/////////////////////////////////////////////////// ////////////////////////////
5ba32b457d38d289771609.jpeg
excerpt from main

private void initTab() {
        viewPager = findViewById(R.id.main_view_pager);
        adapter = new TabsFragmentPageAdapter(getSupportFragmentManager(), this);
        adapter.addFragment(new MainFragmentCreateMenu());
        adapter.addFragment(new MainFragmentWatchProgress());
        adapter.addFragment(new MainFragmentAddProductMenu());
        viewPager.setAdapter(adapter);
        tabLayout = findViewById(R.id.main_tab_layout);
        tabLayout.setupWithViewPager(viewPager);
    }

the adapter itself
public class TabsFragmentPageAdapter extends FragmentPagerAdapter {

    private String[] titleFragment;

    public TabsFragmentPageAdapter(FragmentManager fm, AppCompatActivity app) {
        super(fm);
        this.titleFragment = app.getResources().getStringArray(R.array.main_menu);
    }

    private final List<Fragment> mFragmentList = new ArrayList<>();

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return titleFragment[position];
    }

    @Override
    public int getCount() {
        return titleFragment.length;
    }

    public void addFragment(Fragment fragment) {
        mFragmentList.add(fragment);

    }
}

the fragment itself
public class MainFragmentCreateMenu extends Fragment {

    private View rootView;
    private FragmentTransaction transaction;
    private CreateMenuDate menuDate;
    private ViewGroup container;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                             @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.create_menu, container, false);
        this.container = container;
        if (savedInstanceState == null) { //тут при возврате в этот фрагмент повторно всё вызывается
            transaction = getChildFragmentManager().beginTransaction();
            menuDate = new CreateMenuDate();
            transaction.add(R.id.create_menu_main_container, menuDate);

            transaction.add(R.id.create_menu_main_container, new CreateMenuCardViewForItem());

            transaction.commit();
        }
        return rootView;
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vlad Kovalev, 2018-09-20
@Neonoviiwolf

OnSaveInstantState is called only when the configuration changes (as you wrote, when you rotate the screen, the condition is met) and the fragment is destroyed by the system. FragmentPagerAdapter never removes a fragment from RAM, even if it is not visible in the UI. Try using FragmentStatePagerAdapter and don't forget to implement the OnSaveInstanceState method

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question