问题描述
我正在阅读如何在UI和后台线程之间进行交互。
I am reading about how to interact between UI and background thread here.
本文有以下注释:
我不明白什么是无头的分段。
I dont understand what is retained headless fragment.
例如,通过这种方式我可以添加片段:
For example, in this way I can add fragment:
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.frame, new MyFragment());
transaction.commit();
在片段中我可以像这样执行AsyncTask:
And in fragment I can execute AsyncTask like this:
MyTask myTask = new MyTask();
String[] args = {"Hello"};
myTask.execute(args);
这是否称为在保留的无头片段中声明AsyncTask?
Is this called "to declare the AsyncTask in a retained headless fragment"?
推荐答案
无头片段只是一个没有视图的片段。在片段lifeCycle的 onCreate()
中,使用 setRetainInstance(true);
。即使活动重新创建,这也不会破坏片段。因此,如果AsyncTask在片段中运行,则在重新创建活动时,您将不会丢失AsyncTask。
Headless fragment is nothing but a fragment which does not have a view. In onCreate()
of the fragment lifeCycle, use setRetainInstance(true);
. This will not destroy the fragment even if the activity recreates. So if an AsyncTask is running in fragment, on recreation of the activity, you wont lose the AsyncTask.
在 onCreate
对于活动,您必须添加带有标记的片段。在添加之前,使用 getFragmentManager()。findFragmentByTag(TAG)
检查片段是否存在,如果片段为null,则创建片段的新实例并添加它。
在Fragment中不会有任何视图膨胀,因此无需覆盖 onCreateView()
。
In onCreate
of the activity, you have to add the fragment with a tag. Before adding, check if the fragment exist using getFragmentManager().findFragmentByTag(TAG)
, if the fragment is null then create a new instance of the fragment and add it.In Fragment there will not be any view inflated, so no need to override onCreateView()
.
headlessFragment的一个例子:
An example of headlessFragment :
public class HeadlessProgressFragment extends Fragment {
private ProgressListener mProgressListener;
private AsyncTask<Void, Integer, Void> mProgressTask;
public interface ProgressListener {
void updateProgress(int progress);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
public void setProgressListener(Context context) {
mProgressListener = (ProgressListener) context;
}
public void startProgress(final int size) {
if (mProgressTask == null || mProgressTask.getStatus() != AsyncTask.Status.RUNNING || mProgressTask.getStatus() == AsyncTask.Status.FINISHED) {
mProgressTask = new AsyncTask<Void, Integer, Void>() {
@Override
protected Void doInBackground(Void... params) {
for (int index = 0; index < size; index++) {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
publishProgress(index + 1);
}
}
return null;
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (mProgressListener != null) {
mProgressListener.updateProgress(values[0]);
}
}
};
mProgressTask.execute();
}
}
}
在活动中这样的事情:
In Activity Something like this :
public class MainActivity extends FragmentActivity implements HeadlessProgressFragment.ProgressListener {
private static final String TAG = "progress_fragment";
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dummy_view);
mHeadlessProgressFragment = (HeadlessProgressFragment) getSupportFragmentManager().findFragmentByTag(TAG);
if (mHeadlessProgressFragment == null) {
mHeadlessProgressFragment = new HeadlessProgressFragment();
getSupportFragmentManager().beginTransaction().add(mHeadlessProgressFragment,TAG).commit();
}
mHeadlessProgressFragment.setProgressListener(this);
mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
final Button startFillBtn = (Button) findViewById(R.id.btn_start_filling);
startFillBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mHeadlessProgressFragment.startProgress(100);
}
});
}
@Override
public void updateProgress(int progress) {
mProgressBar.setProgress(progress);
}
}
这篇关于Android-遗留无头片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!