2

Since I implemented a BottomAppBar my App isn't responding when a certain Fragment should get displayed even though I haven't changed anything. There isn't an error message either and all other Fragments which are getting displayed when another item in the BottomNavigationBar is clicked work fine.

Fragment:

public class StatisticsFragment extends Fragment {

private TabLayout mTabLayout;
private ViewPager mViewPager;

private BankAccountsStatisticsFragment bankAccountsStatisticsFragment = new BankAccountsStatisticsFragment();
private BillsStatisticsFragment billsStatisticsFragment = new BillsStatisticsFragment();
private CategoriesStatisticsFragment categoriesStatisticsFragment = new CategoriesStatisticsFragment();
private GoalsStatisticsFragment goalsStatisticsFragment = new GoalsStatisticsFragment();


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View inflatedView = inflater.inflate(R.layout.fragment_statistics, container, false);

    mTabLayout = (TabLayout) inflatedView.findViewById(R.id.tl_statistics);
    mViewPager = (ViewPager) inflatedView.findViewById(R.id.vp_statistics);

    mViewPager.setAdapter(new Adapter(getChildFragmentManager()));
    mTabLayout.setupWithViewPager(mViewPager);

    return inflatedView;
}

private class Adapter extends FragmentPagerAdapter {

    private static final int TABS = 4;

    public Adapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0: return bankAccountsStatisticsFragment;
            case 1: return billsStatisticsFragment;
            case 2: return categoriesStatisticsFragment;
            case 3: return goalsStatisticsFragment;
            default: throw new IllegalStateException("Couldn't find a fragment for position " + position);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position){
            case 0: return getString(R.string.tab_bank_accounts);
            case 1: return getString(R.string.tab_bills);
            case 2: return getString(R.string.tab_categories);
            default: return getString(R.string.tab_goals);
        }
    }

    @Override
    public int getCount() {
        return TABS;
    }
}

}

LOGCAT

LOGCAT

eli2003
  • 400
  • 5
  • 13

1 Answers1

0

Inflating a Layout and creating a Fragment transition (like the "FragmentManager....replace().commit()") are done in the MainThread where the UserInterface is rendered, so the UserInterface will freeze in these moments. I think you have to pre-load the "bad" Fragment and just HIDE it instead of Destroy it...in this way the next time you want to create it is already ready.

emandt
  • 2,547
  • 2
  • 16
  • 20