第二次调用片段使其不可见
Calling a fragment a second time makes it invisible
果然如标题所说。我已经创建了一个应用程序,我将把它的代码放在下面,当我从自动生成的 Home 片段中调用片段时,该片段会在您选择模板 Navigation Drawer Activity
时生成,它会很好地弹出,当我滑动时回来又消失了。
但是,当我第二次按下按钮调用片段时,它只是显示一个空白屏幕。
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;
}
}
它在代码中有注释,但我会在这里再说一遍 nextFrag.color = Color.LTGRAY;
几乎从来没有完全相同的颜色,因此每次都不同,所以定义一次片段并再次调用同一个片段当一个按钮被调用时不是一个可行的选择。
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>
如果需要更多文件,请告诉我。
它仍然执行被调用片段中的代码,因为它仍然在控制台中将 System.out.println("But it does output");
输出为 But it does output
。
但是,第二次按下按钮时,它不显示片段。
任何帮助将不胜感激。
忘记了MainActivity.java
中onBackPressed()
方法中的fm.popBackStackImmediate();
。其中,当实际放置在 onBackPressed()
中时,一切都完美无缺。
果然如标题所说。我已经创建了一个应用程序,我将把它的代码放在下面,当我从自动生成的 Home 片段中调用片段时,该片段会在您选择模板 Navigation Drawer Activity
时生成,它会很好地弹出,当我滑动时回来又消失了。
但是,当我第二次按下按钮调用片段时,它只是显示一个空白屏幕。
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;
}
}
它在代码中有注释,但我会在这里再说一遍 nextFrag.color = Color.LTGRAY;
几乎从来没有完全相同的颜色,因此每次都不同,所以定义一次片段并再次调用同一个片段当一个按钮被调用时不是一个可行的选择。
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>
如果需要更多文件,请告诉我。
它仍然执行被调用片段中的代码,因为它仍然在控制台中将 System.out.println("But it does output");
输出为 But it does output
。
但是,第二次按下按钮时,它不显示片段。
任何帮助将不胜感激。
忘记了MainActivity.java
中onBackPressed()
方法中的fm.popBackStackImmediate();
。其中,当实际放置在 onBackPressed()
中时,一切都完美无缺。