【问题标题】:android - Fragment onCreateView not called after update and commiting transactionandroid - 更新和提交事务后未调用片段 onCreateView
【发布时间】:2016-10-27 08:36:55
【问题描述】:

更改数据后,我无法获取要更新的片段。我打电话给

getFragmentManager().beginTransaction().detach(this).attach(this).commit();

但是 onCreateView 没有被调用。我在 refreshFragment() 中尝试了几种不同的方法。在我输入上述代码行之后,我确实看到了“I/InjectionManager:dispatchCreateOptionsMenu”的消息,所以发生了一些事情,但没有调用 onCreateView()。

 

public class MainActivityFragment extends Fragment {
private static Logger logger = Logger.getLogger(MainActivityFragment.class);
String poolWord = "ABCDE";
String newWord = "";

public MainActivityFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    logger.info("onCreate------------------------");
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);

 }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    logger.info("onCreateView------------------------");
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    GridView gridViewPool = (GridView) view.findViewById(R.id.gridviewPool);
    gridViewPool.setAdapter(new LetterImageAdapter(getActivity(), poolWord));
    gridViewPool.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            logger.error("po    " + position);
            updateWords(position);
            refreshFragment();
        }
    });
    GridView gridviewNewWord = (GridView) view.findViewById(R.id.gridviewNewWord);
    gridviewNewWord.setAdapter(new LetterImageAdapter(getActivity(), ""));
    return view;
}

private void refreshFragment() {
    logger.info("refreshFragment------------------------");
       getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    // getFragmentManager().beginTransaction().replace(R.id.fragment, this).attach(this).commit();
    // Fragment fragment = getFragmentManager().findFragmentById(R.id.fragment);
    //getFragmentManager().beginTransaction().detach(fragment).attach(fragment).commit();

    getFragmentManager().executePendingTransactions();
}

【问题讨论】:

  • 我修改了 refreshFragment() 并将其分解为单独的行,然后添加了一些日志记录以查看片段是否已分离。 isDetached 在我调用 transaction.detach() 之前和之后都返回 false。我是新手,所以不确定会发生什么

标签: android android-fragments


【解决方案1】:

几个小时后,我开始工作了。不确定所有细节,因为这是我的第一个 android 项目。发布答案以防其他人遇到同样的问题。

我怀疑问题的症结在于 Intellij 生成的代码没有在 Activity 类中启动事务。它不包括

 if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new MainActivityFragment())
                .commit();
    }

我不确定如果没有这个,我的 MainActivityFragment 是如何创建的,但确实如此。我还将生成的 activity-main.xml 替换为我从教程中获得的一个。 Intellij 的那个没有我可以用于容器对象的 android:id。它还包含我目前不需要的其他内容。新的 activity-main.xml 如下所示:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
         android:layout_width="match_parent" android:layout_height="match_parent"
         tools:context=".MainActivity" tools:ignore="MergeRootFrame"/>

旧生成的是:

<android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

</android.support.design.widget.CoordinatorLayout>

我还删除了一个 content-main.xml。这可能是 android 用来在没有显式代码的情况下生成我的 MainActivityFragment 的原因。对android开发更有经验的人可能能够回答这个问题。

  <fragment xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/fragment"
      android:name="com.ultramondo.mywordandroid.MainActivityFragment"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      app:layout_behavior="@string/appbar_scrolling_view_behavior"
      tools:layout="@layout/fragment_main">

  </fragment>

我确信有一些有用的东西我可以回去,但现在我想要简单。我还必须进行一些更改才能将 android.support.v4.app 包用于 Fragment 和 Transaction 类。这有点像进入电影中间并弄清楚情节是什么。看完整部电影,相信会更清晰。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多