2

In my StartActivity the BottomNavigationBar Listener has the following setup:

private GuideFragment guideFragment = new GuideFragment();
private MapFragment mapFragment = new MapFragment();
private MoreFragment moreFragment = new MoreFragment();

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        Fragment selectedFragment = null;
        switch (item.getItemId()) {
            case R.id.navigation_guide:
                selectedFragment = guideFragment;
                break;
            case R.id.navigation_map:
                selectedFragment = mapFragment;
                break;
            case R.id.navigation_more:
                selectedFragment = moreFragment;
                break;
        }
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.content, selectedFragment);
        transaction.commit();
        return true;
    }

};

As I mentioned above I want to prevent that the selected fragments always reloads the sources/view. I tried out some stuff like - in the fragments:

if (rootView == null)
     inflater.inflate...

But the fragments still recreate the view and load (in my case) webresources new.

I read something that a PageView could help, especially

offScreenPageLimit

should do the magic.

My main question is where should I implement a PageViewer - Is it possible in my StartActivity? Or can I solve the problem in an other way?

Tobias Alt
  • 453
  • 1
  • 3
  • 11
  • Are your fragments contents directly or tangentially related? That is, can the contents of each fragment exist and make sense without the others? If not, then perhaps switching a tabbed navigation with a `ViewPager` would better suit your requirements. – CzarMatt Jun 05 '17 at 19:09
  • All three fragments are in a different view/tab. Is View Pager then an own class or can I add it in my activity? – Tobias Alt Jun 05 '17 at 19:11
  • Or that I understand it correctly - Should I have to remove the BottomNavigation and replace it with a ViewPager? – Tobias Alt Jun 05 '17 at 19:12
  • Yes - consider the _type_ of content you are showing and choose the best widget to show that content. Review https://material.io/guidelines/components/bottom-navigation.html and then the ViewPager docs. Probably `ViewPager` would work better for your requirements. – CzarMatt Jun 05 '17 at 19:14
  • 1
    Okay thanks CzarMatt. But I can not really believe that Android/Google team provides a BottomNavigationBar without handling fragment lifecycles. Sure it is a bit new stuff - but always call a new instance of a tab is... – Tobias Alt Jun 05 '17 at 19:17

1 Answers1

4

I did it boys! There is no ViewPager necessary.

Here is my solution (all coded in StartActivity not in Fragments):

private final GuideFragment guideFragment = new GuideFragment();
private final MapFragment mapFragment = new MapFragment();
private final MoreFragment moreFragment = new MoreFragment();
private final android.app.FragmentManager fm = getFragmentManager();
Fragment active = guideFragment;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_guide:
                if(active != guideFragment) {
                    fm.beginTransaction().show(guideFragment).commit();
                }
                else {
                    fm.beginTransaction().hide(active).show(guideFragment).commit();
                }
                active = guideFragment;
                break;
            case R.id.navigation_map:
                fm.beginTransaction().hide(active).show(mapFragment).commit();
                active = mapFragment;
                break;
            case R.id.navigation_more:
                fm.beginTransaction().hide(active).show(moreFragment).commit();
                active = moreFragment;
                break;
        }

        return true;
    }

};

and in onCreate list the transaction commits.

fm.beginTransaction().add(R.id.content,moreFragment).commit();
fm.beginTransaction().add(R.id.content, mapFragment).commit();
fm.beginTransaction().add(R.id.content, guideFragment).commit();

It is very important to commit the first tabs fragment last(fragm3,fragm2,fragm1) if you have 3 tabs.

Highly speed performance on the smartphone now by not loading every fragment new/refresh.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
Tobias Alt
  • 453
  • 1
  • 3
  • 11