【发布时间】:2015-07-08 18:51:31
【问题描述】:
我们早在 10 月份就开发了一个新版本的应用程序,当时没有注意到这个问题,所以可能没有发生。这段代码没有改变,但改变的是Android的版本。我们甚至在运行 4.4.2 的设备上看到了这种情况,所以这不仅仅是棒棒糖的问题。
我有一个“静态”类,它在整个应用程序中用于显示和关闭进度对话框。该类的相关方法包含在下面的代码块中。我已经围绕有问题的 API 调用添加了一些时间,并且还包括了几个调用的结果。 progressDialog 是类上的一个静态字段,用于跟踪当前打开的对话框,以便将其关闭。
/**
* Shows the progress dialog box, allowing for the message and title to be supplied.
* The progress dialog will be indeterminate and not cancelable.
*
* @param context The context that owns the dialog.
* @param message The message to display in the dialog.
* @param title The title for the dialog box.
*/
public static void showProgressDialog ( Context context, String message, String title ) {
closeProgressDialog();
String actualTitle = OverrideProgressDialogTitle ? DefaultProgressDialogTitle : title;
String actualMessage = OverrideProgressDialogMessage ? DefaultProgressDialogMessage : message;
Date start = new Date();
progressDialog = ProgressDialog.show( context, actualTitle, actualMessage, true, false );
Date end = new Date();
Log.v( LogTag, String.format("It took %s minutes to show the dialog.", DateUtils.millisecondsToMinutes( end.getTime() - start.getTime() ) ) );
}
/**
* Closes the progress dialog box if it is opened.
*/
public static void closeProgressDialog () {
if ( progressDialog != null && progressDialog.isShowing() ) {
try {
progressDialog.dismiss();
}
catch ( Exception e ) {
Log.e( LogTag, "Error closing dialog.", e );
}
}
progressDialog = null;
}
以下是一些时间安排:
It took 0.0013666668 minutes to show the dialog.
It took 3.8333333E-4 minutes to show the dialog.
It took 3.6666667E-4 minutes to show the dialog.
It took 1.4308 minutes to show the dialog.
结果非常可重复...第一次在应用程序中的 2 个特定屏幕上显示对话框时,从调用 ProgressDialog.show 返回大约需要 1.5 分钟,但随后调用显示来自那些对话框同样的地方几乎立即返回。在一种情况下,从片段的 onStart 方法显示对话框以加载数据,在另一种情况下,当单击将数据提交到服务器的按钮时显示该对话框。
有没有人觉得这个方法调用需要很长时间才能返回,如果是这样,你是如何解决的?
编辑:我实际上对此进行了进一步的追踪,这是导致挂断的其他问题。 AsyncTask 的 onPreExecute 方法完成和 doInBackground 开始之间有时似乎有很长的延迟。
【问题讨论】:
-
持有对 ProgressDialog 的静态引用会泄露 Activity 上下文,这可能会导致各种麻烦。
-
对话框总是在同一个活动中打开和关闭,所以应该没有泄漏。
标签: android