【问题标题】:Fragment not showing when add() method is called调用 add() 方法时不显示片段
【发布时间】:2013-12-01 14:18:40
【问题描述】:

这里是声明:

FragmentManager fr = getFragmentManager();
FragmentTransaction ft = fr.beginTransaction();
Fragment myFragment = new defaultFragment();
ft.add(R.id.fragment, myFragment);
ft.commit();

这里是onClick() 方法:

public void startFragment(View v){
    newFragment = new nextFragment();
    execute();
}

执行方法:

public void execute() {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    transaction.add(R.id.fragment, newFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

我不能解决这个问题,但是当我单击触发startFragment(View v) 方法的按钮时,什么也没有发生,片段不会被我要添加到堆栈上的片段替换。我认为问题与这条线有关transaction.add(R.id.fragment, newFragment);请帮助谢谢:)

【问题讨论】:

  • 使用replace。您是否还在日志中看到任何异常
  • 在logcat上,好像调用了nextFragment()的类,但是当前的fragment没有消失,也没有出现下一个fragment,我没有用replace,因为我不想丢我从上一个片段中获得的数据,所以当我按下后退按钮时,我不必重新加载 onCreateView()。
  • 嘿@Raghunandan 我明白了,谢谢你的帮助 :)) 感谢 Rajesh Batth

标签: android transactions fragment


【解决方案1】:

尝试将片段添加到FrameLayout。我无法将其添加到 LinearLayout,但我将其更改为 FrameLayout 并且它起作用了。

【讨论】:

  • 哇,哈哈!非常感谢!多么愚蠢..当然,LinearLayout 会在前一个片段的旁边或下方添加视图。 FrameLayout 会将其放在前一个的 TOP 上。所以它在那里,但不可见,因为第一个片段填满了屏幕(使用 MATCH_PARENT)
  • 有问题就杀了我哈哈
  • 但是假设实现 100 个事务,堆栈会包含所有片段吗?这不是记忆的问题吗@Rajesh Batth
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多