Layout Inflater 膨胀 EditText 时有趣的线程行为
Interesting thread behavior when Layout Inflater inflates EditText
当通过设备管理器查看我的应用程序时,我遇到了充气器的行为,我希望堆栈溢出社区中的某个人可以帮助我理解。我有一个片段,当它使用布局充气器来充气 EditText 时,它会产生两个异步任务。为什么会产生这些,我能做些什么来防止这种情况或之后关闭它们?
当我注释掉 EditText 时,片段加载后我立即看到:
放回去后:
我的片段看起来像:
public class SearchFragment implements View.OnClickListener{
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String TAG = "HistorySearchFragment";
private static final String ARG_SECTION_NUMBER = "sectionNumber";
public static SearchFragment newInstance(int sectionNumber) {
SearchFragment fragment = new SearchFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_search, container, false)
return view;
}
}
fragment_search.xml看起来像
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.main.SearchFragment"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/search_button_bw"
android:layout_below="@+id/space"
android:isScrollContainer="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
/*************************************************
THIS TEXT BOX HERE
/************************************************/
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
/*************************************************
To here
/************************************************/
</RelativeLayout>
</ScrollView >
<Button
android:id="@+id/search_button_bw"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="Search"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我已经证实,只有当这个片段加载并且任务每次都成功生成时才会发生这种情况。有谁知道为什么会发生这种情况以及我该如何预防?
Does anyone have any idea why this is happening
TextView
有 references to AsyncTask
in its locale handling(至少在其 Android 5.1 版本中;您需要检查 Android 的特定版本 运行宁).
AsyncTask
will initialize a thread pool when the AsyncTask
class is loaded 作为设置其余 static
数据成员的一部分。该池的最小池大小为 CPU 核心数加一。因此,在模拟器或单核硬件上,一旦某些东西引用 AsyncTask
class,就会创建两个 Thread
对象。它们将在您的流程期间存在。请注意,在多核 CPUs 上,我希望在稳定状态下该池中有更多线程,并且可以有更多线程(最多是内核数量的两倍,再加上一个)如果很多的任务处于活动状态。
how I can prevent it?
不要使用 TextView
或任何继承自它的东西。另外,不要使用引用 AsyncTask
的任何其他框架 classes。
由于这些线程不是 运行ning(正如您从屏幕截图中看到的那样),我看不出您的问题是什么。如果有的话,我们需要更多框架中使用后台线程的东西,因为有些地方Android的框架代码运行与[=19冲突=]线程检查。
what can I do to... close them afterwards?
它们是关闭的,只要它们不是 运行ning。正如您在屏幕截图中看到的,它们已被阻止 ("Wait")。他们将保持该状态,直到需要 运行 一项任务。这就是线程池的工作方式。
当通过设备管理器查看我的应用程序时,我遇到了充气器的行为,我希望堆栈溢出社区中的某个人可以帮助我理解。我有一个片段,当它使用布局充气器来充气 EditText 时,它会产生两个异步任务。为什么会产生这些,我能做些什么来防止这种情况或之后关闭它们?
当我注释掉 EditText 时,片段加载后我立即看到:
放回去后:
我的片段看起来像:
public class SearchFragment implements View.OnClickListener{
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String TAG = "HistorySearchFragment";
private static final String ARG_SECTION_NUMBER = "sectionNumber";
public static SearchFragment newInstance(int sectionNumber) {
SearchFragment fragment = new SearchFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.fragment_search, container, false)
return view;
}
}
fragment_search.xml看起来像
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
tools:context="com.main.SearchFragment"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/search_button_bw"
android:layout_below="@+id/space"
android:isScrollContainer="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
/*************************************************
THIS TEXT BOX HERE
/************************************************/
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
/*************************************************
To here
/************************************************/
</RelativeLayout>
</ScrollView >
<Button
android:id="@+id/search_button_bw"
android:layout_width="match_parent"
android:layout_height="70dp"
android:text="Search"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
我已经证实,只有当这个片段加载并且任务每次都成功生成时才会发生这种情况。有谁知道为什么会发生这种情况以及我该如何预防?
Does anyone have any idea why this is happening
TextView
有 references to AsyncTask
in its locale handling(至少在其 Android 5.1 版本中;您需要检查 Android 的特定版本 运行宁).
AsyncTask
will initialize a thread pool when the AsyncTask
class is loaded 作为设置其余 static
数据成员的一部分。该池的最小池大小为 CPU 核心数加一。因此,在模拟器或单核硬件上,一旦某些东西引用 AsyncTask
class,就会创建两个 Thread
对象。它们将在您的流程期间存在。请注意,在多核 CPUs 上,我希望在稳定状态下该池中有更多线程,并且可以有更多线程(最多是内核数量的两倍,再加上一个)如果很多的任务处于活动状态。
how I can prevent it?
不要使用 TextView
或任何继承自它的东西。另外,不要使用引用 AsyncTask
的任何其他框架 classes。
由于这些线程不是 运行ning(正如您从屏幕截图中看到的那样),我看不出您的问题是什么。如果有的话,我们需要更多框架中使用后台线程的东西,因为有些地方Android的框架代码运行与[=19冲突=]线程检查。
what can I do to... close them afterwards?
它们是关闭的,只要它们不是 运行ning。正如您在屏幕截图中看到的,它们已被阻止 ("Wait")。他们将保持该状态,直到需要 运行 一项任务。这就是线程池的工作方式。