【问题标题】:Android ListFragment invoked twiceAndroid ListFragment 调用了两次
【发布时间】:2013-02-22 23:13:54
【问题描述】:

当我从 Activity 启动片段时,onCreate()onViewCreated() 方法会被调用两次。

这是我的代码:

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

    FragmentManager frgManager = getSupportFragmentManager();
    if (findViewById(R.id.fl_chapter_detail) != null)
    {
        mbThreePaneLayout = true;
        FragmentChapterList frgChapterList = (FragmentChapterList) frgManager.findFragmentById(R.id.frg_chapter_list);
        frgChapterList.setActivateOnItemClick(true);
    }

    if (savedInstanceState == null)
    {
        String strSelectedSection = getIntent().getStringExtra(Konstant.KEY_SELECTED_SECTION);

        if (((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) != Configuration.SCREENLAYOUT_SIZE_XLARGE) || (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT))
            currentActionBar.setTitle(strSelectedSection);

        Bundle arguments = new Bundle();
        arguments.putString(Konstant.KEY_SELECTED_SECTION, strSelectedSection);

        FragmentChapterList frgChapterList = new FragmentChapterList();
        frgChapterList.setArguments(arguments);

        FragmentTransaction frgTransaction = frgManager.beginTransaction();
        frgTransaction.add(R.id.frg_chapter_list, frgChapterList).commit();
    }
}

有人知道为什么会这样吗?

【问题讨论】:

  • 您是否可能设置了类属性并且在包含的活动中具有片段插入代码?
  • 我在活动中设置了名称属性和片段插入代码。如果我删除 name 属性,它就会崩溃。
  • 好的,我知道现在是什么问题了;看我的回答。

标签: android android-fragments android-listfragment android-fragmentactivity


【解决方案1】:

解决这个问题:

  • 将片段的标签类型从更改为占位符,例如
  • 删除name 属性。

欲了解更多信息,请参阅:InflateException Caused by <fragment> Tag Without a Bound Class

【讨论】:

  • 你的意思是name属性?因为即使在 Fragment 标记中我也没有类属性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-07
  • 1970-01-01
相关资源
最近更新 更多