简体   繁体   English

后退按钮时片段错误

[英]fragment wrong when back button

I have 3 fragment, but when first I load the fragment one and then fragment two and when I press the back button, why the fragment one and two are concat? 我有3个片段,但是当我先加载片段1然后又加载片段2时,当我按下返回按钮时,为什么片段1和片段2是concat? for example picture below: 例如下面的图片:

maybe, I must destroy the last fragment for new fragment. 也许,我必须销毁最后一个片段以换成新片段。 how to fix this issue? 如何解决这个问题? thanks. 谢谢。

错误视图片段

Overide the on back pressed in you fragment and put you code in there. 覆盖压在片段中的背面,然后将代码放在那里。

public class MyFragment extends BaseFragment {

  /**
  * Back pressed send from activity.
  *
  * @return if event is consumed, it will return true.
  */
  @Override
  public boolean onBackPressed() {
    startActivity(new Intent(this, blabla.class));
    //or you could say finish();
    //or you could say moveTaskToBack(true);

} }

Check the following possible conditions, please .... 请检查以下可能的条件...。

  • Check which Fragment method you have used, replace or Add 检查您使用, replace or Add片段方法

     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.rlContainer, fragment); // replace the existing one 
  • Enabled backStack support and then you explicitly load the same fragment on backPress 启用backStack支持,然后在backPress上显式加载相同的片段

     FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.rlContainer, fragment); //ft.addToBackStack(null); // will add to the fragment stack ft.commitAllowingStateLoss(); 

If you are enabled backStack support you can check the count 如果启用了backStack支持,则可以检查计数

FragmentManager fm = getFragmentManager(); // or 'getSupportFragmentManager();'
int count = fm.getBackStackEntryCount();
for(int i = 0; i < count; ++i) {    
   fm.popBackStack();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM