是否应该通过 AsyncTask class 调用 DialogFragment?
Should DialogFragment be called through AsyncTask class?
我看了很多指南,但我仍然很困惑。
我在某处读到 "activity flow" 不应被 DialogFragment 打断,因此您应该在 AsyncTask Class 中调用 DialogFragment ]里面ActivityClass.
在其他指南中,我看到从 Activity Class 调用 DialogFragment 时未使用 AsyncTask。
所以我的问题是:是否应该只通过 AsyncTask 调用 DialogFragment class?
到目前为止我就是这样做的; Activity class 代码:
public class LunchActivity extends AppCompatActivity {
....
public void callDialog(){
class ShowInfoToUser extends AsyncTask<Bundle, Void, Bundle> {
...
@Override
protected Bundle doInBackground(Bundle... args) {
...
}
@Override
protected void onPostExecute(Bundle resultBundle) {
DialogFragment permissionDialogManager= permissionDialogManager.newInstance(messageBundle);
permissionDialogManager.show(activity.getSupportFragmentManager(), "Permission Dialog");
}
}
}
这是扩展 DialogFragment 的 class:
public class PermissionDialogManager extends DialogFragment {
public static PermissionDialogManager newInstance(Bundle bundle) {
PermissionDialogManager frag = new PermissionDialogManager();
frag.setArguments(bundle);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
谢谢
由于 Dialog 会打断用户,我认为没有理由将它放在 AsyncTask 中。该对话框不应花费大量时间自行生成。
AsyncTask 的 onPostExecute 方法中的代码在 UI 线程中执行。在您提供的示例中,是否使用 AsynTask 没有区别,因为代码将在 UI 线程中执行。
也许在您看到的示例中,它们在 AsyncTask 的 doInBackground 方法(在单独的线程中执行)中处理一些信息,然后在 onPostExecute 方法中它们使用先前的信息来调用 DialogFragment。
如何知道什么时候应该 运行 在 UI 线程中编写代码?
When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.
我看了很多指南,但我仍然很困惑。
我在某处读到 "activity flow" 不应被 DialogFragment 打断,因此您应该在 AsyncTask Class 中调用 DialogFragment ]里面ActivityClass.
在其他指南中,我看到从 Activity Class 调用 DialogFragment 时未使用 AsyncTask。
所以我的问题是:是否应该只通过 AsyncTask 调用 DialogFragment class?
到目前为止我就是这样做的; Activity class 代码:
public class LunchActivity extends AppCompatActivity {
....
public void callDialog(){
class ShowInfoToUser extends AsyncTask<Bundle, Void, Bundle> {
...
@Override
protected Bundle doInBackground(Bundle... args) {
...
}
@Override
protected void onPostExecute(Bundle resultBundle) {
DialogFragment permissionDialogManager= permissionDialogManager.newInstance(messageBundle);
permissionDialogManager.show(activity.getSupportFragmentManager(), "Permission Dialog");
}
}
}
这是扩展 DialogFragment 的 class:
public class PermissionDialogManager extends DialogFragment {
public static PermissionDialogManager newInstance(Bundle bundle) {
PermissionDialogManager frag = new PermissionDialogManager();
frag.setArguments(bundle);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
...
}
}
谢谢
由于 Dialog 会打断用户,我认为没有理由将它放在 AsyncTask 中。该对话框不应花费大量时间自行生成。
AsyncTask 的 onPostExecute 方法中的代码在 UI 线程中执行。在您提供的示例中,是否使用 AsynTask 没有区别,因为代码将在 UI 线程中执行。
也许在您看到的示例中,它们在 AsyncTask 的 doInBackground 方法(在单独的线程中执行)中处理一些信息,然后在 onPostExecute 方法中它们使用先前的信息来调用 DialogFragment。
如何知道什么时候应该 运行 在 UI 线程中编写代码?
When your app performs intensive work in response to user interaction, this single thread model can yield poor performance unless you implement your application properly. Specifically, if everything is happening in the UI thread, performing long operations such as network access or database queries will block the whole UI. When the thread is blocked, no events can be dispatched, including drawing events. From the user's perspective, the application appears to hang. Even worse, if the UI thread is blocked for more than a few seconds (about 5 seconds currently) the user is presented with the infamous "application not responding" (ANR) dialog. The user might then decide to quit your application and uninstall it if they are unhappy.