How To Call A Toast Message On Success Callback In Class
Solution 1:
Every app has its own special thread that runs UI objects such as View objects; this thread is called the UI thread. Only objects running on the UI thread have access to other objects on that thread. Because tasks that you run on a thread from a thread pool aren't running on your UI thread, they don't have access to UI objects. To move data from a background thread to the UI thread, use a Handler that's running on the UI thread or can use android implementation for the same as shown here.
- Case 1
MyActivity.this.runOnUiThread(new Runnable() {
@Override
voidrun() {
Toast.makeText(MyActivity.this,
"message", Toast.LENGTH_LONG).show();
});
- Case 2
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
publicvoidrun() {
Toast.makeText(MyActivity.this,
"message", Toast.LENGTH_LONG).show();
}
});
Had it been Main thread you would have used it directly like
Toast.makeText(MyActivity.this,
"message", Toast.LENGTH_LONG).show();
Solution 2:
This callback is an asynchronous function, and you can change View just in UI-thread, so Handler
will be helpd for you.
....
privatefinalstaticintMSG_SUCCESS=0x0001;
privatefinalstaticintMSG_FAIL=0x0002;
privateHandlerhandler=newHandler(){
@OverridepublicvoidhandleMessage(Message msg) {
switch(msg.what){
case MSG_SUCCESS:
//Toast successbreak;
case MSG_FAIL:
//Toast failbreak;
default:
break;
}
}
};
......
......
if (response.isSuccessful()) {
Log.i("SUCCESSFUL", "onSuccess: data uploaded");
handler.sendEmptyMessage(MSG_SUCCESS);
} else {
Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
handler.sendEmptyMessage(MSG_FAIL);
}
......
Solution 3:
You are getting error
java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
Because you're calling it from a worker thread. You need to call Toast.makeText()
(and most other functions dealing with the UI) from within the main thread. You could use a handler,
@OverridepublicvoidonResponse(Call call, Response response)throws IOException {
if (response.isSuccessful()) {
Log.i("SUCCESSFUL", "onSuccess: data uploaded");
context.runOnUiThread(newRunnable() {
publicvoidrun() {
Toast.makeText(context, "SUCCESSFUL", Toast.LENGTH_SHORT).show();
}
});
}
Solution 4:
Just send the context of your activity while calling this method:
voidmethodName(Context c){
post(URL, jsonData, newCallback() {
@OverridepublicvoidonFailure(Call call, IOException e) {
Log.i("FAILED", "onFailure: Failed to upload data to server");
//here I want to show toast message
}
@OverridepublicvoidonResponse(Call call, Response response)throws IOException {
if (response.isSuccessful()) {
Log.i("SUCCESSFUL", "onSuccess: data uploaded");
Toast.makeText(c,"message",Toast.LENGTH_SHORT).show();
//here I want to show toast message
} else {
Log.i("UN SUCCESSFUL", "onFailure: Failed to upload data to server");
//here I want to show toast message
}
}
});
}
Solution 5:
Try this
TaskActivity.this.runOnUiThread(newRunnable() {
@Overridevoidrun() {
Toastmsg= Toast.makeText(TaskActivity.this,
"message", Toast.LENGTH_LONG);
msg.show();
});
Post a Comment for "How To Call A Toast Message On Success Callback In Class"