Android -Retained headless fragment(Android - 保留的无头片段)
问题描述
我正在此处阅读有关如何在 UI 和后台线程之间进行交互的信息.
I am reading about how to interact between UI and background thread here.
本文有以下注意事项:
AsyncTask 不会自动处理配置更改,即如果活动被重新创建.程序员必须处理在他的编码中.对此的常见解决方案是声明 AsyncTask在保留的无头片段中.
The AsyncTask does not handle configuration changes automatically, i.e. if the activity is recreated. The programmer has to handle that in his coding. A common solution to this is to declare the AsyncTask in a retained headless fragment.
我不明白什么是保留的无头片段.
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"?
推荐答案
Headless fragment 只不过是一个没有视图的 fragment.在片段生命周期的onCreate()
中,使用setRetainInstance(true);
.即使活动重新创建,这也不会破坏片段.因此,如果 AsyncTask 在片段中运行,则在重新创建 Activity 时,您不会丢失 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)
检查片段是否存在,如果片段为空,则创建片段的新实例并添加它.在 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();
}
}
}
在活动中是这样的:
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 - 保留的无头片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 保留的无头片段
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01