简体   繁体   中英

Returning from Asynch Task to MainActivity

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 doInBackground task 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?

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
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM