2016-09-06 81 views
1

在我的MainActivity中有一個FrameLayout(id = fragment_container),它被onCreate中的活動替換爲FragmentAFragmentA只包含一個按鈕並在MainActivity中調用onClick以便用FragmentB替換FrameLayoutFragmentB包含一個RecyclerView並顯示正確。我打電話fragmentManager.popBackStack()帶回FragmentA。現在FragmentA顯示在FragmentB之上(我仍然可以與之交互)。popBackStack後仍然可見片段

爲什麼在撥打popBackStack後不能刪除FragmentB

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    private FragmentManager fragmentManager; 

    @Override 
    public void onBackPressed() { 
     fragmentManager.popBackStack(); 
    } 

    @Override 
    public void onClick(View view) { 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     fragmentTransaction.replace(R.id.fragment_container, new FragmentB()); 
     fragmentTransaction.addToBackStack(null); 
     fragmentTransaction.commit(); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     //... 

     fragmentManager = getSupportFragmentManager(); 
     FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     FragmentA fragment = FragmentA.getInstance(this); //register OnClickListener 
     fragmentTransaction.replace(R.id.fragment_container, fragment); 
     fragmentTransaction.commit(); 
    } 
//... 
} 

FragmentA

public class FragmentA extends Fragment { 

    private View.OnClickListener onClickListener; 

    public FragmentA() { 
    } 

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

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_a, container, false); 

     FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab); 
     fab.setOnClickListener(onClickListener); 

     return view; 
    } 

    public static FragmentA getInstance(View.OnClickListener onClickListener) { 
     FragmentA fragmentA = new FragmentA(); 
     fragmentA.setOnClickListener(onClickListener); 
     return fragmentA; 
    } 

    private void setOnClickListener(View.OnClickListener onClickListener) { 
     this.onClickListener = onClickListener; 
    } 
} 

FragmentB

public class FragmentB extends Fragment { 

    //... 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.FragmentB, container, true); 
     //manipulating Views... 
     //... 

     return view; 
    } 
} 
+0

爲片段A添加背景顏色。 – JpCrow

+0

@JPCrow會不會是一個骯髒的解決方法? –

+0

如果您願意,您可以將其添加到您的活動主題中,然後使用開發人員工具檢查該片段的透支 – JpCrow

回答

0

的問題是在FragmentB類。你不應該通過參數attachToRoot=true

試試這個:

public class FragmentB extends Fragment { 

    //... 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
     //don't pass true 
     View view = inflater.inflate(R.layout.FragmentB, container, false); 
     //manipulating Views... 
     //... 

     return view; 
    } 
} 
0

使用popBackStackImmediate代替,

@Override 
public void onBackPressed() { 
    fragmentManager.popBackStackImmediate(); 
} 

popBackStackImmediate docs

像popBackStack(),但立即執行調用內部的操作。這就像之後調用executePendingTransactions()。

+0

已經嘗試過,沒有任何更改。 –