简体   繁体   English

android-更新和提交事务后未调用片段onCreateView

[英]android - Fragment onCreateView not called after update and commiting transaction

I am unable to get a fragment to update after changing data. 更改数据后,我无法获取要更新的片段。 I call 我打电话

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

but onCreateView does not get called. 但不会调用onCreateView。 I tried several different ways in refreshFragment(). 我在refreshFragment()中尝试了几种不同的方法。 I do see messages for "I/InjectionManager: dispatchCreateOptionsMenu" after I put in the above line of code so something is happening, but onCreateView() is not getting called. 我在上面的代码行中输入了“ 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(); }

After several hours, I got it to work. 几个小时后,我开始工作了。 Not sure of all the details as this is my first android project. 不确定所有细节,因为这是我的第一个android项目。 Posting an answer in case anybody else has the same problem. 如果其他人有同样的问题,请发布答案。

The crux of the problem, I suspect, is the generated code from Intellij did not start a transaction in the Activity class. 我怀疑问题的症结在于,Intellij生成的代码没有在Activity类中启动事务。 It did not include 它不包括

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

I'm not sure how my MainActivityFragment was created without this, but it was.I also replaced the generated activity-main.xml with one I had from a tutorial. 我不知道如何创建MainActivityFragment而不使用它,但是确实如此。我还用教程中的内容替换了生成的activity-main.xml。 The one from Intellij did not have an android:id that I could use for the container object. Intellij的一个没有可用于容器对象的android:id。 It also contained other stuff I did not need at this time.The new activity-main.xml looks like this: 它还包含了我此时不需要的其他内容。新的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"/>

The old generated one was: 旧生成的是:

<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>

There was also a content-main.xml which I got rid of. 还有一个我删除的content-main.xml。 That may be what android used to generate my MainActivityFragment without explicit code. 这可能就是android用来在没有显式代码的情况下生成我的MainActivityFragment的原因。 Someone more experienced with android development may be able to answer that. 对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>

I'm sure there's some useful stuff I can go back to but for now I want simplicity. 我敢肯定,我可以回溯一些有用的东西,但是现在我想要简单。 I also had to make some changes to use android.support.v4.app packages for Fragment and Transaction classes. 我还必须进行一些更改,才能将android.support.v4.app包用于Fragment和Transaction类。 It's kind of like coming in the middle of a movie and figuring out what the plot is. 就像进入电影中间并弄清楚剧情是什么。 Once I see the whole movie, I'm sure it will become clearer. 一旦看到整部电影,我相信它将变得更加清晰。

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

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