简体   繁体   中英

Calling a fragment a second time makes it invisible

So it is exactly as the title suggests. I've created an app of which I will put the code beneath here, and when I call a fragment from the automaticly generated Home fragment which generates when you choose the template Navigation Drawer Activity , it pops up fine, and when I swipe back it dissapears again.

However, then when I press the button to call a fragment the second time, it just shows an empty screen.

MainActivity.java:

package eu.sleepy.emptyfragmentbug2;

import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.widget.FrameLayout;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.fragment.app.FragmentManager;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public void onBackPressed(){
        FragmentManager fm = getSupportFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            System.out.println("popping backstack");

            if (fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName().equals("findThisFragment")) {
                View linearLayout = findViewById(R.id.homefrag);
                linearLayout.setVisibility(View.VISIBLE);

                FrameLayout frameLayout = findViewById(R.id.blankfrag);
                frameLayout.setVisibility(View.GONE);
            }
        } else {
            System.out.println("nothing on backstack, calling super");
            super.onBackPressed();
        }
    }
}

HomeFragment.java

package eu.sleepy.emptyfragmentbug2.ui.home;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import eu.sleepy.emptyfragmentbug2.BlankFragment;
import eu.sleepy.emptyfragmentbug2.R;

public class HomeFragment extends Fragment {

    private HomeViewModel homeViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        final TextView textView = root.findViewById(R.id.text_home);
        homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });

        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BlankFragment nextFrag = new BlankFragment();

                nextFrag.color = Color.LTGRAY; // This value is always different when the button is pressed, so calling the same fragment wouldn't work.
                getParentFragment().getFragmentManager().beginTransaction()
                        .replace(((ViewGroup)getView().getParent()).getId(), nextFrag, "findThisFragment")
                        .addToBackStack("findThisFragment")
                        .commit();

                View linearLayout = getActivity().findViewById(R.id.homefrag);
                linearLayout.setVisibility(View.GONE);
            }
        };

        Button button = root.findViewById(R.id.button);
        button.setOnClickListener(onClickListener);
        Button button2 = root.findViewById(R.id.button2);
        button2.setOnClickListener(onClickListener);

        return root;
    }
}

It is commented in the code, but I'll say it here again nextFrag.color = Color.LTGRAY; is almost never the exact same color and thus is different every time, so defining the fragment once and calling the same fragment again when one button is called is not a viable option.

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/homefrag"
    tools:context=".ui.home.HomeFragment">

    <TextView
        android:id="@+id/text_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="63dp"
        android:layout_marginTop="155dp"
        android:layout_marginEnd="89dp"
        android:layout_marginBottom="153dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/text_home"
        app:layout_constraintEnd_toStartOf="@+id/button2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="89dp"
        android:layout_marginTop="158dp"
        android:layout_marginEnd="73dp"
        android:layout_marginBottom="150dp"
        android:text="Button"
        app:layout_constraintBottom_toTopOf="@+id/text_home"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/button"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

BlankFragment.java

package eu.sleepy.emptyfragmentbug2;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class BlankFragment extends Fragment {

    public Integer color;

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

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

        View root = inflater.inflate(R.layout.fragment_blank, container, false);

        FrameLayout frameLayout = root.findViewById(R.id.blankfrag);
        frameLayout.setVisibility(View.VISIBLE);

        // Inflate the layout for this fragment
        return root;
    }

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

        System.out.println("But it does output");
        CreateImage();
    }

    public void CreateImage() {
        FrameLayout frameLayout = getActivity().findViewById(R.id.blankfrag);
        frameLayout.setVisibility(View.VISIBLE);

        ImageView imageView = new ImageView(getContext());
        imageView.setBackgroundColor(color);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(300, 500);
        layoutParams1.setMargins(25, 0, 0,0);
        imageView.setLayoutParams(layoutParams1);

        frameLayout.addView(imageView);
    }
}

fragment_blank.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/blankfrag"
    tools:context=".BlankFragment">

</FrameLayout>

If there is a need for more files, please let me know.

It still executes the code in the called fragment, because it still outputs the System.out.println("But it does output"); in the console as But it does output .

Yet, it doesn't display the fragment when the button is pressed for a second time.

Any help would be really appreciated.

I forgot the fm.popBackStackImmediate(); in the onBackPressed() method in the MainActivity.java . Which, when actually placed in onBackPressed() made everything work perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM