How to show toast in AsyncTask in doInBackground

后端 未结 9 764
灰色年华
灰色年华 2020-11-27 23:01

In one of my activities I\'m using AsyncTask. In doInBackground() I\'m making calls to various methods. In one of these methods I\'m getting an exc

9条回答
  •  星月不相逢
    2020-11-27 23:27

    Write the following code where you have to show toast in doInBackground() method

    runOnUiThread(new Runnable() {
    
    public void run() {
    
      Toast.makeText(getApplicationContext(), "Example for Toast", Toast.LENGTH_SHORT).show();
    
       }
    });
    
    • BTW: if you are using Fragments, you need to call runOnUiThread(...) through your activity:

    getActivity().runOnUiThread(...)

提交回复
热议问题