0

I have an adapter class that handles displaying a list of thumbnail images. When the user clicks on a thumbnail, it retrieves an image from a URL and takes about a second to do so and displays it in a dialog fragment. Because of the delay, I want there to be a toast that says "fetching image". However, the toast does not appear until the dialog fragment displays, which is pointless.

I have tried moving the toast before and after the call to make a dialog fragment and still the same result. I have tried using an AsyncTask to synchronize the toast first and then the dialog fragment but still the same result.

Adapter class:

holder.viewThumbnail.setOnClickListener(v ->
        {
        FetchImage fetchImage = new FetchImage(MainActivity.mainActivity, rootView, position);
        fetchImage.execute();
        });

FetchImage class:

protected final Void doInBackground(WeakReference<Activity>... params)
        {
        if(MainActivity.animalList.get(position).getImage() == null)
            {
            MainActivity mainActivity = weakReferenceActivity.get();
            if(mainActivity != null)
                {
                new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(mainActivity, mainActivity.getString(R.string.fetching_image), Toast.LENGTH_SHORT).show());
                }
            }
        return null;
        }

protected void onPostExecute(Void result)
        {


        super.onPostExecute(result);
        MainActivity mainActivity = weakReferenceActivity.get();
        if(mainActivity != null)
            {
            mainActivity.dialogShow(view, C.NO, C.DIALOG_IMAGE, "", "", position);
            }
        }

I also simply tried doing this in the adapter class without the AsyncTask route:

Adapter class:

holder.viewThumbnail.setOnClickListener(v ->
        {
        if(MainActivity.animalList.get(position).getImage() == null)
            interfaceCommon.makeToast("fetching data", 1);
        interfaceCommon.dialogShow(rootView, C.NO, C.DIALOG_IMAGE, "", "", position);
        });

The interfaceCommon is simply a common interface to call methods in the Main Activity. I have one activity for this app and multiple fragments.

Samuel
  • 395
  • 2
  • 5
  • 20
  • don't do strange things. pass a listener into the `AsyncTask`'s constructor and let the `MainActivity` implement that interface, which can show a `Toast` or `Snackbar`, as soon as the task triggers the listener. besides, your `AsyncTask` should not depend that much on `MainActivity`, in general. – Martin Zeitler Dec 15 '18 at 03:37
  • That seems way stranger passing a listener from a view Holder into an AsyncTask class – Samuel Dec 15 '18 at 03:58
  • Did you try overriding onPreExecute() and toasting from there? – Greg Moens Dec 15 '18 at 04:34
  • Yes it does the same thing even in onPreExecute. – Samuel Dec 15 '18 at 04:43
  • @Samuel by what you are doing there, you completely defeat the idea of an `AsyncTask` ...and what I've suggested is the quasi-default way of doing that, proven to be working. – Martin Zeitler Dec 15 '18 at 04:47
  • You can't toast in asyncTask without a reference to an activity – Samuel Dec 15 '18 at 04:49
  • The toast has to be done on the UI thread. I first tried to do all this without asyncTask but it still exhibits the same behavior: the toast doesn't show until the dialog fragment does – Samuel Dec 15 '18 at 04:52
  • Actually for a toast, you only need a reference to a context, it doesn't have to be an activity. Did you try getting a reference to the application and using that as your context? – Greg Moens Dec 15 '18 at 04:57
  • That is a weak reference to the Main Activity. I only have one activity. A reference is passed to the FetchImage class in the constructor. Like I said, I don't need to do this in an AsyncTask. I merely thought that I could sync the events better if I did so. – Samuel Dec 15 '18 at 05:06
  • I've just tried to display a progress circle on the screen in lieu of the toast and it too doesn't show until the dialog fragment comes on the screen. It seems no animation will display until the fragment is in view. – Samuel Dec 15 '18 at 21:25

0 Answers0