Question

I have many Activities and AsyncTasks in my application. I want to define an Interface with a method which accept a Generic AsyncTask and a boolean. I will implement that interface in all Activities and want to call the Interface's method from AsyncTask.

How should I declare method which accept all the subclasses of AsyncTask? I want to make it Generic so I can use instanceof operator to determine which asyncTask called the Override method in activity. I tried to use:

public onUpdate(AsyncTask<Params, Progress, Result> task,boolean show);

but is not allowed.

Était-ce utile?

La solution

You can declare it this way:

public void onUpdate(AsyncTask<?, ?, ?> task, boolean show) {

}

You can then use it by extending an AsyncTask:

private class SomeTask extends AsyncTask<String, Integer, Long> {

    @Override
    protected Long doInBackground(String... params) {
        // TODO Auto-generated method stub
        return null;
    }

}

and calling your method:

onUpdate(new SomeTask(), true);

Autres conseils

You can declare the method as follows...

public void onUpdate(AsyncTask<Object, Object, Object> task,boolean show);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top