androidandroid-fragmentstabmenu

Blank fragment after navigation with BottomNav and TabMenu


I am developing an Android app that has a little bit confusing navigation structure which leads me to my problem. There are two ways to navigate through the app. First is the BottomNav and second is a TabMenu. I thought about working with fragments that replace each other, so I came up with the following structure:

1. BottomNav#1
  - TabMenu#1
  - Tabmenu#2
  - TabMenu#3
  - ...
2. BottomNav#2
  - TabMenu#4
  - Tabmenu#5
  - TabMenu#6
  - ...
... and so on.

The problem I am facing is that when I navigate from BottomNav#1 to BottomNav#2 and back again there is a blank screen that doesn't show the content of the actual fragment:

When I open the app

After clicking on BottomNav#2 and then back to BottomNav#1 the fragment seems to be blank

My guess is that I somehow have a problem with my fragmentTransaction.replace(); as it seems like the fragment doesn't get loaded again? I am kind of new to this and really tried to find an answer online but this is a bit specific why I guess I didn't find anything.

This is my MainActivity:

package com.example.XXXX;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

private FrameLayout mMainFrame;

private AusweisFragment ausweisFragment;
private SpendenFragment spendenFragment;

@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ausweisFragment = new AusweisFragment();
    spendenFragment = new SpendenFragment();
    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

}

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

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.navigation_ausweis:

                setFragment(ausweisFragment);
                return true;
            case R.id.navigation_spenden:

                setFragment(spendenFragment);
                return true;
        }
        return false;
    }
};

private void setFragment(Fragment fragment)
{
    FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.main_frame, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

}

This should lead to e.g. the fragment called "ausweis" by clicking on on BottomNav#1 (switch case):

package com.example.XXXXXXX;



import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.TabItem;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class AusweisFragment extends Fragment {


private View rootView;
private AusweisPageAdapter ausweisPageAdapter;
private TabLayout tabLayout;
private ViewPager viewPager;

public AusweisFragment() {
    // Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState)
{
    rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
    tabLayout = rootView.findViewById(R.id.tablayoutAusweis);                            
    viewPager = rootView.findViewById(R.id.viewPagerAusweis);                            
    ausweisPageAdapter = new AusweisPageAdapter(getActivity().getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(ausweisPageAdapter);                                             
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));


    tabLayout.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) {

        }
    });
    return rootView;
}

}

After that I would like to call another fragment called "spenden" which is exactly the same as the fragment called "ausweis" but with different named tabs, so I think it is not bother you with more code.

edit: I missed about writing where the "content" I wrote about gets from. For a first try and proof that the fragments change, I hardcoded a phrase like "Ausweis" into the XML which is connected to my fragment java class.

Maybe one of you has an idea to that problem. I think it has something to do with my onCreateView in one of the fragments, but I have no close clue.

Hopefuly I didn't miss an important detail. I am very grateful for any kind of help. Thanks a lot in advance.


Solution

  • The answer to my problem was to use a child-parent relation between the different fragments. With the click on BottomNav#1 I am inflating an fragment which inflates a new fragment inside itself. That was the key problem. In my code I handled this as a second "normal" getFragmentManager();.

    The answer is to use getChildFragmentManager(); for the nested fragment instead. Like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
    
        rootView= inflater.inflate(R.layout.fragment_ausweis, container, false);
        tabLayout1 = rootView.findViewById(R.id.tablayoutAusweis);                           
        viewPager = rootView.findViewById(R.id.viewPagerAusweis);                           
        ausweisPageAdapter = new AusweisPageAdapter(getChildFragmentManager(), tabLayout1.getTabCount());
        viewPager.setAdapter(ausweisPageAdapter);                                            
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout1));
    ...
    ...
    

    Hopefuly this will help some people when they face the same problem with nested fragments.