Android - progressdialog not showing up in AsyncTask

I have an Android app that I'm having problems with.

Basically the ProgressDialog is not displayed at all. I believe this is a threading problem, but I don't know how to fix it.

I am using ActionBarSherlock with some Fragments . I also use the new Android DrawerLayout , where I have my options in the box that replace the fragment when clicked.

When I first download my application, I want to check the database to see if the original data is loaded. If not, then I quit and start AsyncTask to load the data. During this SHOULD have a ProgressDialog display, but it does not.

Can anyone see where I'm wrong? Thanks.

MainScreen - default landing page / fragment when opening the application

 public class MainScreen extends SherlockFragment { public static final String TAG = "MainScreen"; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_main, container, false); setHasOptionsMenu(false); ImageView imgLogo = (ImageView) rootView.findViewById(R.id.imgMainScreen); imgLogo.setOnClickListener(new ButtonHandler(getActivity())); checkDatabase(); return rootView; } private void checkDatabase() { //Ensure there is data in the database DBHelper db = new DBHelper(this.getSherlockActivity()); db.checkDatabase(); } ... } 

DBHelper.checkDatabase () - the method that initiates the download

 public void checkDatabase() { if (isEmpty()) { //Connect to net and download data NetworkManager nm = new NetworkManager(activity); if (!nm.downloadData()) { Toast.makeText(activity, R.string.internetCheck, Toast.LENGTH_SHORT).show(); } } } 

and finally NetworkManager.downloadData () is a method starting with AsyncTask :

  public boolean downloadData() { try { return new HttpConnection(activity).execute().get(); } catch (InterruptedException e) { e.printStackTrace(); } catch (ExecutionException e) { e.printStackTrace(); } return false; } public class HttpConnection extends AsyncTask<Void, Void, Boolean> { private ProgressDialog progressDialog; private Activity m_activity; protected HttpConnection(Activity activity) { m_activity = activity; } @Override protected void onPreExecute() { progressDialog = new ProgressDialog(m_activity); progressDialog.setMessage("Wait ..."); progressDialog.setCancelable(false); progressDialog.setMax(100); progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); progressDialog.show(); super.onPreExecute(); } @Override protected Boolean doInBackground(Void... params) { String[] types = new String[]{"type1", "type2", "type3", "type4", }; StringBuilder sb = new StringBuilder(); for(String type : types) { sb = new StringBuilder(); if(DBHelper.TYPE4_TABLE.equals(type)) { InputStream is = activity.getResources().openRawResource(R.raw.dbdata); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); try { sb.append(reader.readLine()); } catch (IOException e) { Toast.makeText(activity.getApplicationContext(), "Error retriveving data", Toast.LENGTH_SHORT).show(); Log.e(Constants.TAG, "Error reading data"); e.printStackTrace(); } } else { sb = fetchURLData(Constants.ALL_URL+type); } cleanDataAndStore(sb, type); } return true; } @Override protected void onPostExecute(Boolean result){ progressDialog.hide(); } } 

Using the code above, all I get is a white screen when I try to load the application, and sometimes ANR. When the download is completed, the fragment is loaded. Therefore, it works great except for the missing ProgressDialog .

PS. Note that I set activity in each constructor.

Thanks.

+4
source share
2 answers

Remove .get() from return new HttpConnection(activity).execute().get(); . You basically block your UI thread. After removal, it should work, since AsyncTasks is expected to work.

The goal is to be asynchronous, so boolean downloadData() should have a return type of void . If you need to do something with the data, then you must implement the listener interface and pass it to AsyncTask.

Listener example:

 class TaskConnect extends AsyncTask<Void, Void, ConnectionResponse> { private final AsyncTaskListener mListener; /** * */ public TaskConnect(AsyncTaskListener listener) { ... mListener = listener; } @Override protected void onPreExecute() { if (mListener != null) { mListener.onPreExecute(mId); } } @Override protected ConnectionResponse doInBackground(Void... cData) { ... return responseData; } @Override protected void onPostExecute(ConnectionResponse response) { if (mListener != null) { mListener.onComplete(response); } else { LOG.w("No AsyncTaskListener!", new Throwable()); } } } public interface AsyncTaskListener { public abstract void onPreExecute(int id); public abstract void onComplete(ConnectionResponse response); } 
+10
source

My problem was not a common problem for others where they called the get () method after the execute () method. My problem was in the Context, which I switched to my AsyncTask method. I have anActivity parameter, and I have ReadMeActivity, which invokes the asynctask task. Instead of using the context in which it was called (ReadMeActivity.this), I used the Active settings that prevented it from seeing. As soon as I switched it and gave it the context in which the activity was named, it worked.

Hope this helps someone else.

0
source

Source: https://habr.com/ru/post/1497072/


All Articles