android asynctask sending callbacks to ui
You can create an interface
, pass it to AsyncTask
(in constructor), and then call method in onPostExecute()
For example:
Your interface:
public interface OnTaskCompleted{
void onTaskCompleted();
}
Your Activity:
public class YourActivity implements OnTaskCompleted{
// your Activity
}
And your AsyncTask:
public class YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
private OnTaskCompleted listener;
public YourTask(OnTaskCompleted listener){
this.listener=listener;
}
// required methods
protected void onPostExecute(Object o){
// your stuff
listener.onTaskCompleted();
}
}
EDIT
Since this answer got quite popular, I want to add some things.
If you're a new to Android development, AsyncTask
is a fast way to make things work without blocking UI thread. It does solves some problems indeed, there is nothing wrong with how the class works itself. However, it brings some implications, such as:
- Possibility of memory leaks. If you keep reference to your
Activity
, it will stay in memory even after user left the screen (or rotated the device). AsyncTask
is not delivering result toActivity
ifActivity
was already destroyed. You have to add extra code to manage all this stuff or do you operations twice.- Convoluted code which does everything in
Activity
When you feel that you matured enough to move on with Android, take a look at this article which, I think, is a better way to go for developing your Android apps with asynchronous operations.
Callback from asynctask back to activity
you can create an interface
to make a callback.
Example:
create a class AsyncTaskMessage
:
public class AsyncTaskMessage extends AsyncTask<Void, Void, Void> {
Context context;
public OnMessageListener mListener;
public interface OnMessageListener {
void messageCallback(ArrayList<String> messageList); // you can change the parameter here. depends on what you want.
}
public AsyncTaskMessage(Context context) {
this.context = context;
mListener = (OnMessageListener) context;
}
@Override
public void onPreExecute() {
}
@Override
protected void onPostExecute() {
// your code goes here for the callback.
// messageList - A list of messages.
mListener.messageCallback(messageList);
}
}
then your MessageCentre
class:
public class MessageCentre {
Context context;
public void MessageCentre(Context context) {
this.context = context;
}
public void getMessages(int id) {
// You said your asyncTask goes here.
AsyncTaskMessage async = new AsyncTaskMessage(context);
async.execute();
}
}
then your activity where the ArrayAdapter
// A class where your ArrrayAdapter.
public class MainActivity extends AppCompatActivity implements AsyncTaskMessage.OnMessageListener {
//..
public void displayMessage() {
MessageCentre centre = new MessageCentre(this);
centre.getMessages(1);
}
@Override
public void messageCallback(ArrayList<String> messageList) {
Log.i("MainActivity", "Response: " + messageList.toString());
ArrayAdapter a = new ArrayAdapter(getApplicationContext(),R.id.item_layout, messageList);
listview.setAdapter(a);
}
}
create new async task with sending callback to ui
The activity should implement the receiver and the async task should call it, something like this:
public class MyActivity extends Activity implements CallbackReceiver{
@Override
public void onCreate(Bundle savedInstanceState) {
//Set everything up
MyAsyncTask task = new MyAsyncTask();
task.setOnDataReceivedListener(this);
task.execute();
}
@Override
public void onReceiveData(String data){
//Do something with the data
}
}
Then, in the async task, you can call the receiver method in onPostExecute
public abstract class MyAsyncTask extends AsyncTask<String, String, String>{
private CallbackReciever receiver = null;
public void setOnDataReceivedListener(CallbackReciever receiver){
this.receiver = receiver
}
protected void onPostExecute(String file_url)
{
if(receiver != null){
receiver.onReceiveData(file_url);
}
}
}
The second way to do this is to simply make an anonymous inner class of your async task and override onPostExecute:
public class MyActivity extends Activity implements CallbackReceiver{
@Override
public void onCreate(Bundle savedInstanceState) {
//Set everything up
MyAsyncTask task = new MyAsyncTask(){
@Override
protected void onPostExecute(String file_url){
//Do what you want with your data here
}
};
task.execute();
}
}
Creating a callback function using AsyncTask
If you want to utilize a callback for an AsyncTask you can handle it via the following.
Do something like this (modifying your code to add what is below)
public class DataCollectClass extends AsyncTask<Object, Void, JSONObject> {
public interface OnDataCollectedCallback{
void onDataCollected(JSONObject data);
}
private OnDataCollectedCallback mCallback;
public DataCollectClass(OnDataCollectedCallback callback){
mCallback = callback;
}
// your code that is already there
...
@Override
public onPostExecute(JSONObject response){
if(mCallback != null)
mCallback.onDataCollected(response);
}
}
Then to make the magic happen
new DataCollectClass(new OnDataCollectedCallback() {
@Override
public void onDataCollected(JSONObject data) {
if(data != null)
// DO something with your data
}
}).execute(requestURI, formVars);
However, it is worth noting, most networking libraries, including OkHttp, handle background threads internally, and include callbacks to utilize with the requests.
This also implements a custom interface, so others may be able to see how you could use this for any AsyncTask.
Asynctask (execute from alarmmanager) callbacks to ui
Firstly I'd recommend using an IntentService
instead of an AsyncTask
.
Secondly, create an inner class in your main activity which extends BroadcastReceiver
.
Register an instance of the inner BroadcastReceiver
in onResume()
of your Activity
and unregister it in onPause()
.
Have your first BroadcastReceiver
start the IntentService
and have the IntentService
send a broadcast which the inner BroadcastReceiver
of your Activity
will be listening for if the Activity
is running - the inner BroadcastReceiver
can then create the progress bar.
When the IntentService
finishes what it needs to do get it to send another broadcast to indicate that the progress bar should be dismissed.
UI working problem in async task in Android
I solved the problem just as follows by myself.
Thank you for your help.
https://stackoverflow.com/a/9963705/12817071
how can i call back my parent activity in Asynctask
In the OnPostExecute method of the AsyncTask, start the new activity via an Intent and pass the loaded data along to it.
@Override
protected void onPostExecute(String data) {
Intent intent = new Intent(MainActivity.this, WhateverActivityToStart.class);
intent.putExtra("SOME_DATA", data);
MainActivity.this.startActivity(intent);
}
You'll need to get familiar with starting an activity and passing data between activities
Android: Callback AsyncTask to Fragment(Not Activity)
Without taking your code in consideration I will post the most essential to make a functional callback.
TestFragment:
public class TestFragment extends Fragment {
/* Skipping most code and I will only show you the most essential. */
private void methodThatStartsTheAsyncTask() {
TestAsyncTask testAsyncTask = new TestAsyncTask(new FragmentCallback() {
@Override
public void onTaskDone() {
methodThatDoesSomethingWhenTaskIsDone();
}
});
testAsyncTask.execute();
}
private void methodThatDoesSomethingWhenTaskIsDone() {
/* Magic! */
}
public interface FragmentCallback {
public void onTaskDone();
}
}
TestAsyncTask:
public class TestAsyncTask extends AsyncTask<Void, Void, Void> {
private FragmentCallback mFragmentCallback;
public TestAsyncTask(FragmentCallback fragmentCallback) {
mFragmentCallback = fragmentCallback;
}
@Override
protected Void doInBackground(Void... params) {
/* Do your thing. */
return null;
}
@Override
protected void onPostExecute(Void result) {
mFragmentCallback.onTaskDone();
}
}
Related Topics
"Conversion to Dalvik Format Failed With Error 1" on External Jar
Maintain/Save/Restore Scroll Position When Returning to a Listview
Viewpager and Fragments - What's the Right Way to Store Fragment'S State
How to Add Dividers and Spaces Between Items in Recyclerview
Integrating the Zxing Library Directly into My Android Application
Converting a View to Bitmap Without Displaying It in Android
How to Create Custom Ratings Bar in Android
Adb Server Version Doesn't Match This Client
Android Listview With Different Layouts For Each Row
How to Have Multiple Styles Inside a Textview
What Is the List of Supported Languages/Locales on Android
Android Toolbar Center Title and Custom Font
How to Update Gradle in Android Studio