Skip to content
Advertisement

Android Studio fragment becomes invisible by clicking on menu item

I’m working on a nutrition app.

In this app at the bottom there is a menu with the menu items “Start”, “Gerichte” (Meals) and “Info”. When you click on the menu item “Gerichte” it opens a fragment which contains a menu at the top with the menu items “Hauptgerichte” (Main Dishes) and “Nebengerichte” (Side dishes). Depending on which menu item you click in the top menu it opens a different fragment within the “Gerichte” fragment.

But when you are already in the “Gerichte” fragment and then click on the menu item “Gerichte” again in the bottom menu the fragments of the menu items in the top menu become invisible.
Here a video that shows the problem clearly: https://www.youtube.com/watch?v=lLV4cSIULwU

How can I fix that?

Here the code of the java class with the “Gerichte” fragment.

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import de.robinp2000.ernhrungsrechner.MainActivity;
import de.robinp2000.ernhrungsrechner.R;
import de.robinp2000.ernhrungsrechner.databinding.FragmentInfoBinding;
import de.robinp2000.ernhrungsrechner.databinding.FragmentMealsBinding;
import de.robinp2000.ernhrungsrechner.info.InfoViewModel;

public class MealsFragment extends Fragment  {

    private MealsViewModel mealsViewModel;
    private FragmentMealsBinding binding;

    private PageViewModel pageViewModel;
    private SectionsPagerAdapter sectionsPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabs;

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

        mealsViewModel =
                new ViewModelProvider(this).get(MealsViewModel.class);

        binding = FragmentMealsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        sectionsPagerAdapter = new SectionsPagerAdapter(getActivity(),
                getActivity().getSupportFragmentManager());


        viewPager = binding.viewPager;
        viewPager.setAdapter( sectionsPagerAdapter );

        tabs = binding.tabs;
        tabs.setupWithViewPager(viewPager);

        return root;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

      /*  pageViewModel = new ViewModelProvider( getActivity() ).get( PageViewModel.class );
        pageViewModel.setIndex( 1 );*/
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        getActivity().getSupportFragmentManager().getFragments().clear();

        PagerAdapter adapter = viewPager.getAdapter();
        adapter.notifyDataSetChanged();

        viewPager.setAdapter( null );
        pageViewModel = null;
        binding = null;
    }

}

And the code of my main activity:



import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.android.material.tabs.TabLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import de.robinp2000.ernhrungsrechner.databinding.ActivityMainBinding;
import de.robinp2000.ernhrungsrechner.databinding.FragmentMealsBinding;
import de.robinp2000.ernhrungsrechner.meals.SectionsPagerAdapter;
import de.robinp2000.ernhrungsrechner.profile.Profile;

import static de.robinp2000.ernhrungsrechner.utils.FileManagement.hasString;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getSupportActionBar().hide();

        if ( ! hasString(  this,"weight" ) ) {
            Intent intent = new Intent( MainActivity.this, Profile.class );
            intent.putExtra( "noprofile", true );
            MainActivity.this.startActivity( intent );
            return;
        }

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_meals, R.id.nav_info )
                .build();

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        navController.createDeepLink();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
        NavigationUI.setupWithNavController(binding.navView, navController);

    }

}

Advertisement

Answer

I solved my problem now by removing the code line “view.setAdapter( null );” in the onDestroyView event and putting it into the onStop event of this fragment class!

So my code in the MealsFragment class looks like this:

`package de.robinp2000.ernhrungsrechner.meals;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.material.tabs.TabLayout;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.viewpager.widget.ViewPager;
import de.robinp2000.ernhrungsrechner.databinding.FragmentMealsBinding;

public class MealsFragment extends Fragment  {

    private MealsViewModel mealsViewModel;
    private FragmentMealsBinding binding;

    private SectionsPagerAdapter sectionsPagerAdapter;
    private ViewPager viewPager;
    private TabLayout tabs;

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

        mealsViewModel =
                new ViewModelProvider(this).get(MealsViewModel.class);

        binding = FragmentMealsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        sectionsPagerAdapter = new SectionsPagerAdapter( getActivity(),
                getActivity().getSupportFragmentManager() );


        viewPager = binding.viewPager;
        viewPager.setAdapter( sectionsPagerAdapter );

        tabs = binding.tabs;
        tabs.setupWithViewPager(viewPager);

        return root;
    }

    @Override
    public void onStop() {
        super.onStop();
        viewPager.setAdapter( null );
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

}

`

Advertisement