2014-02-25 30 views
0

嗨顯示我想要的問題,當我使用onBackPressed與片段:如何選擇哪一個片斷必須backpressed

如果以這種方式添加片段:

public void showFragment(Fragment new_fragment,String tag,boolean back){ 
    FragmentManager fm = getSupportFragmentManager(); 
    Fragment fragment = fm.findFragmentByTag(tag); 
    fragment = fragment==null?new_fragment:fragment; 
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.replace(R.id.fragment_container, fragment); 
    if(back)transaction.addToBackStack(tag); 
    transaction.commit(); 
} 

@Override 
public void onBackPressed() { 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction ft = fm.beginTransaction(); 
    if (fm.getBackStackEntryCount() > 0) { 
      fm.popBackStack(); 
    }else { 
     moveTaskToBack(true); 
    } 
} 

當我使用popBackStack碎片將重疊。爲什麼?

如何正確刪除或避免存儲我不需要的背層碎片(例如SplashScreenFragment)?

謝謝!

回答

相關問題