[英]Fragment still visible after popBackStackImmediate and replace
我的堆棧上有fragmentA,屏幕上有fragmentB。 我想用fragmentC替換fragmentB,以便當用戶按下時,我們回到fragmentA。 這就是我用fragmentC替換fragmentB的方法
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_container, fragment);
transaction.commitAllowingStateLoss();
這就是我處理背壓的方式
final FragmentManager fm = getSupportFragmentManager();
if (fm.getBackStackEntryCount() != 0) {
fm.popBackStackImmediate();
} else {
super.onBackPressed();
}
將fragmentB替換為fragmentC后,如果按下后退按鈕,將顯示fragmentA,但fragmentC在屏幕上仍然可見,這給了我一些非常奇怪的UI。 有人知道為什么會這樣嗎?
將事務添加到后台:
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_container, fragment);
transaction.addToBackStack("")
transaction.commitAllowingStateLoss();
如果我正確地理解了您,FragmentA會覆蓋在FragmentC上,因此這兩個片段都是可見的。
如果是這種情況,請在xml布局中設置片段A和C的背景。 這是一個例子
片段A
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tool="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical">
//Fragment A contents
</LinearLayout>
片段C
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tool="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFF"
android:orientation="vertical">
//Fragment C contents
</LinearLayout>
觀察兩個片段在其rootview中都設置了background屬性。 因此,盡管它們被覆蓋了,但一次只能看到一個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.