- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Activities 中确实使用了很多 AsyncTasks 作为私有(private)内部类。从一开始就让我困惑的一件事是我在 onPostExecute() 中获得了什么上下文?
如果您查看下面显示的精简示例,您可以在 onPostExecute() 中使用 getString()。该调用使用什么上下文? Activity 可能在 doInBackground 期间发生了变化(例如 OrientationChange),因此它不可能是那个上下文。
目前我正在修补 Activity 上下文 (task.context = this) 并使用该特定上下文 (context.getString())。
我的问题:在不使用周围 Activity 的上下文的情况下,在内部类 AsyncTask 的 onPostExecute() 中使用 getString() 是否省事?
非常感谢。
public class MyActivity extends Activity {
/* package */ MyActivity context;
private class MyAsyncTask extends AsyncTask<Void, Void, Void> {
public MyAsyncTask(final MyActivity context) {
super();
this.context = context;
}
@Override
protected Cursor doInBackground(/* ... */) {
// ...
}
@Override
protected void onPostExecute(/* ... */) {
if (context != null) {
// Currently I do use that
String s = context.getString(R.string.mytext);
context.task = null;
}
// Would this be save?
String s2 = getString(R.string.mytext);
}
@Override
protected void onPreExecute (/* ... */) {
// ...
}
}
/* package */ MyAsyncTask task;
@Override
public void onCreate(final Bundle bundle) {
// ...
Bundle bundleExtras = getIntent().getExtras();
if (bundleExtras != null) {
task = (MyAsyncTask) getLastNonConfigurationInstance();
if (task != null) {
task.context = this;
// ...
} else {
task = new MyAsyncTask(this);
task.execute();
}
}
}
@Override
public Object onRetainNonConfigurationInstance() {
if (task != null) {
// ...
task.context = null;
}
return task;
}
}
最佳答案
Is it save to use getString() within onPostExecute() of an Inner Class AsyncTask without using the context of the surrounding Activity?
不,这不安全。作为该 Activity 中的内部类,您的 MyAsyncTask
类在创建时将引用 MyActivity
。因此,如果 MyActivity
由于任何原因(如轮换)被终止,在 onPostExecute
回调中,您将以 MyAsyncTask
持有对的引用而告终杀死的 MyActivity
实例。
使用对 Activity
的引用更安全,因为您将该引用更新为始终指向 MyActivity
的有效实例。也许您还想直接在 onRetainNonConfigurationInstance
中使用 null
使 Activity
无效,以确保您不会触摸它(如果 MyAsyncTask
到达 onPostExecute
直到你再次设置 MyActivity
)直到你有一个有效的引用。
关于android - AsyncTask 的 onPostExecute() 中的上下文是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650366/
当我使用 new GiveMeJson2.execute(ref_id).get() 调用异步类时, 我的应用程序进入 doInBackground,创建一个 JSON 字符串并返回它。 此时,我认为
我正在尝试在后台下载一些文件,为此我使用了下载管理器并制作了此方法: /*** media download ***/ public long mediaDownload(ArrayList
我在尝试使用 onPostExecute 来触发这段代码时遇到问题,我对 java 尤其是多线程很陌生,所以如果我正在做的事情完全错误,请随时纠正我...需要学习. 所以问题是这样的: public
以下代码是我出于 Intent 而添加的代码,结果是从我的login.php 返回的内容。但它无法比较成功与否。在任何情况下它都不会进入 if 循环。但它在警报对话框中显示“登录成功”。 @Overr
字符串 descript 的内容显示在 logcat 中,没有任何问题,但不会显示在 OnPostExecute 对话框中的 TextView 上。这是一个空指针异常。我不知道为什么,因为 episo
当我运行我的应用程序时,onPostExecute似乎没有被调用。它没有像应有的那样填充到 UI 中。另外,关于DoInBackground ,任何超过 for 循环的日志消息: for (int i
我有一个单独的类(不是 Activity)来管理图像及其属性。 在这个类(Image.class)中,我还创建了一个 AsynkTask 来从网络下载位图。一切正常,除了 AsyncTask 继续运行
在 php 脚本的支持下,我有一段代码在 Async Task 中运行。我只想检查我的数据库中是否存在电子邮件 ID。我想显示 toast :“电子邮件已存在”(如果在数据库中)。否则另一个 toas
所以我创建了一个 web 服务调用类,它调用我在 AsyncTask 上扩展的 web 服务: public class WebServiceLoginCall extends AsyncTask{
我有一个扩展 AsyncTask 的类,它旨在用作我的应用程序的通用任务管理器类。 问题:奇怪的行为是进度对话框出现,但从未被关闭。 我确信每个任务实例都会调用 onPostExecute(),因为任
更新,请见下文。 如何将我的类 LocationData 和我的 ArrayList listOfObjects 返回到 onPostExecute()?我想在我的 UI 中使用它,现在它在 Asyn
我正在尝试获取 onPostExecute()完成 doInBackground() 后调用的方法方法,但它没有被调用。我该如何解决这个问题? 我已经调试过了,可以看到ArrayList data里面
我有一些控制权,因此我做了一些线程。我想用 asycntask 将一个屏幕的加载栏显示到另一个屏幕。我的问题是加载栏正在快速显示和消失,然后在没有加载的情况下等待同一个屏幕很多bar.And 一段时间
我在 Android 应用程序上做一个登录系统,当我输入错误的帐户时,onPostExecute 方法返回正确的失败消息,但是当我输入一个现有帐户并且登录正确后它仍然是相同的失败消息,我不明白为什么!
public static class TestTask extends AsyncTask { private String stacktrace; public TestTask
我有一个运行良好的 AsyncTask 类,但在这个类中,我在 onPreExecute 中有一个 ProgressDialog,它工作正常,但我需要在完成进程对话框关闭时,我将代码放在 onPost
我不知道如何找出这个空指针异常发生的位置。 按照 Google Play 的建议,崩溃只在用户的物理设备上发生过一次(到目前为止)——我无法在调试环境中重现它。 Google Play 给我的 Sta
我尝试使用此函数返回对象数组: public static JSONEvent[] invokeFunction(String funName, String requestContent) {
我有一个 Activity (RecipiesActivity),通过单击我的应用程序主 Activity 的“下一步”按钮打开。 在 RecipiesActivity onCreate 中,我想在
我有一个看起来像这样的类: internal class MyClass : AsyncTask>>> { protected override void OnPostExec
我是一名优秀的程序员,十分优秀!