简体   繁体   English

在Android片段中未调用OnCreateView

[英]OnCreateView not called in android fragment

I have a listView on the left, and a nested fragment with checkBoxes on the right. 我在左侧有一个listView,在右侧有一个带checkBoxes的嵌套片段。

在此处输入图片说明

The checkBoxes are in an Arraylist declared globally. 复选框位于全局声明的Arraylist中。 The ArrayList is created dynamically in the onCreateView method of the nestedFragment. ArrayList是在nestedFragment的onCreateView方法中动态创建的。

In the onPause method i save the checkboxes state. onPause方法中,我保存了复选框状态。

The onclick listener of the listView, replaces the Fragments is this way: listView的onclick侦听器以这种方式替换了Fragments:

 CheckNodesFragment checkNodesFragment = new CheckNodesFragment();

 FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();


 fragmentTransaction.replace(R.id.child_fragmentB, checkNodesFragment);
 fragmentTransaction.commit();

Problem: When i click the Icarus or whatever listview item double times, the app crashes in the onPause() , since the onCreateView method making all initializations is not be called, but only onPause is called. 问题:当我两次单击Icarus或任何listview项时,该应用程序在onPause()中崩溃,因为未调用进行所有初始化的onCreateView方法,而是仅调用了onPause。

However, when i add and not replace the fragment all the lifecycles methods are called, and onCreateView() too, but when i add the fragment the screen's text becomes more bold with each add: 但是,当我添加而不替换片段时,将调用所有生命周期方法,也调用onCreateView(),但是当我添加片段时,每次添加时屏幕的文本都会变得更加粗体: 在此处输入图片说明

我发现了问题,发现替换片段时,第一个生命周期片段的调用是onPause方法,然后是onStop,onDestroyView等,考虑到这一点,我解决了我的问题。

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

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