因此,正如標題所暗示的那樣。我已經創建了一個應用程式,我將把代碼放在下面,當我從自動生成的 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;它幾乎從來不是完全相同的顏色,因此每次都不同,因此定義一次片段并在呼叫一個按鈕時再次呼叫相同的片段不是一個可行的選擇.
片段主頁.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>
空白片段.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);
}
}
片段空白.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.
然而,當第二次按下按鈕時,它不會顯示片段。
任何幫助將非常感激。
uj5u.com熱心網友回復:
我忘記了fm.popBackStackImmediate();中的onBackPressed()方法MainActivity.java。哪個,當實際放置時,onBackPressed()一切都會完美運行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/424651.html
上一篇:ViewPager2:僅在可見片段時初始化片段,然后保持它們初始化
下一篇:超型別串列中只能出現一個類
