当 AsyncTask 完成时完成调用活动 [英] Finish the calling activity when AsyncTask completes

查看:36
本文介绍了当 AsyncTask 完成时完成调用活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的通话活动:

public class Hello extends Activity {  

public void onCreate(Bundle savedInstanceState) {

    MyTask mt = new MyTask(this);
    mt.execute();
}

现在在 MyTask(一个外部类)中:

Now In MyTask (an external class):

public class MyTask extends AsyncTask<Void, Void, Void> {
private Context mContext;

public MyTask(Context context) {

    mContext = context;
}  

//doinbackground, etc

    protected void onPostExecute() {
    mContext.finish();

}

如果我删除上面的 mContext.finish(),其他事情会按预期工作.
但是如果我调用 mContext.finish() ,我会收到一个错误:The method finish() is undefined for the type Context(Eclipse 不显示我在编写 mContext 时使用了 finish().所以这表明我错误地使用了 finish().)

Other things are working as expected if I remove mContext.finish() above.
But if I'm calling mContext.finish() , I'm getting an error: The method finish() is undefined for the type Context (Eclipse doesn't show finish() when I write mContext. so that suggests I'm using finish() wrongly.)

MyTask 完成任务后我需要做什么来完成调用活动Hello

What do I need to do to finish the calling activity Hello after MyTask completes the task

推荐答案

((Activity)mContext).finish();

Context 转换为 Activity 并调用其 finish() 方法是正确的方法.不知道为什么你想要从 AsyncTask 完成一个 Activity

Would be the correct way to cast a Context to an Activity and call its finish() method. Not sure why you'd want to finish an Activity from an AsyncTask though

这篇关于当 AsyncTask 完成时完成调用活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆