Returning from Asynch Task to MainActivity

蓝咒 提交于 2020-01-03 04:30:10

问题


I read a lot about it and tried many things, without having succes. But it doesn't seem hard at all, so I guess I am missing a little thing.

I got 2 classes, a MainActivity and an asynch task class.
the doInBackgroundtask is working perfectly. But when it is done, I want to program to continue at a certain point in my MainActivity

protected Integer doInBackground(Void... params) {
    try {
        Log.d("control", "ZipHelper.unzip() - File: " + _archive);
        ZipFile zipfile = new ZipFile(_archive);
        for (Enumeration<? extends ZipEntry> e = zipfile.entries(); e
                .hasMoreElements();) {
            ZipEntry entry = (ZipEntry) e.nextElement();
            unzipEntry(zipfile, entry, _outputDir);

        }
    } catch (Exception e) {
        Log.d("control", "ZipHelper.unzip() - Error extracting file "
                + _archive + ": " + e);
        setZipError(true);
    }
    return null;
}
protected void onPostExecute(Integer... result) {
    //Here something like MainActivity.showPicture();
}

I know I must do something with onPostExecute, but I don't know what exactly.
So let's say, I want to show a Toast in my MainActivity after asynch-task is done?


回答1:


Use a Listener interface.

Example :

Listener Interface

public interface AsyncTaskListener
{
    public void onTaskComplete();
}

ZipHelper Class

public class ZipHelper extends AsyncTask<Void, Void, Integer>
{
    private String filename;
    private AsyncTaskListener listener;
    private File file;
    public ZipHelper(String filename, File file, AsyncTaskListener listener)
    {
        this.filename = filename;
        this.file = file;
        this.listener = listener;
    }

    @Override
    protected void onPreExecute()
    {
        //stuff here
    }

    @override
    protected Integer doInBackground(Void... params)
    {
        //Background stuff here
    }

    @Override
    protected void onPostExecute(Integer... result)
    {
        listener.onTaskComplete();
    }
}

MainActivity

public class MainActivity implements AsyncTaskListener
{
    public void onCreate(Bundle savedInstanceState)
    {
        super(savedInstanceState);
        setContentView(R.layout.main_activity);

        //Your stuff

        new ZipHelper(zip[0].mZipFileName, file, MainActivity.this).execute();
    }

    public void onTaskComplete()
    {
        //AsyncTask post stuff
    }
}



回答2:


Without calling a certain method in your MainActivity you can't feasibly start at a certain point in your MainActivity. The point of AsyncTask is to allow your calling Activity to keep going and not hold up the UI. What you can do is pass a context to your AsyncTask and show the Toast in onPostExecute()

public class MyTask extends AsyncTask<Void, Void ,Void> {  // use whatever params you need here
private Context context;

public MyTask(Context c) {
    context = c;
} 

@Override
protected void onPostExecute(Void result) {
     super.onPostExecute(result);
     Toast.makeText(context, "You did it!". Toast.LENGTH_SHORT).show();
    } 

and call it by passing your Context

Mytask task = new MyTask(this);  //or MyActivity.this depending on where you are
task.execute();  // pass params if you need

I suggest using Activity context instead of Application context for Toast



来源:https://stackoverflow.com/questions/16400862/returning-from-asynch-task-to-mainactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!