0

I'm trying to display a Toast when I call the AsyncTask class with new saveWithStickers(BaseActivity.this).execute(); to notify the user that the save process being started, when I run the code below the onPreExecute() method for some logical reason is not called so the Toast does not appear as well,is runOnUiThread the reason for this behavior?

@SuppressLint("StaticFieldLeak")
public class saveWithStickers extends AsyncTask<Void, File, File> {
    File fileSaved;
    Context mContext;

    saveWithStickers(Context context) {
        mContext = context;
    }

    @Override
    protected File doInBackground(Void... voids) {
        fileSaved = FileUtil.getNewFile(BaseActivity.this, "VAPOGRAM");
        if (fileSaved != null)
            runOnUiThread(() -> {
                stickerView.save(fileSaved, true);
            });
        return fileSaved;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        content.setVisibility(View.GONE);
        Toast.makeText(mContext, "saving ...", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onPostExecute(File file) {
        super.onPostExecute(file);
}


saveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckPermissionUtil checkPermissionUtil = new CheckPermissionUtil(BaseActivity.this);
                if (Build.VERSION.SDK_INT >= 23) {
                    if (checkPermissionUtil.checkPermission(1812)) {
                        new saveWithStickers(BaseActivity.this).execute();
                    } else
                        checkPermissionUtil.requestPermission(1812);
                } else {
                    new saveWithStickers(BaseActivity.this).execute();
                }
            }
        });

1 Answers1

1

Replace your code with this

@SuppressLint("StaticFieldLeak")
        public class saveWithStickers extends AsyncTask<Void, File, File> {
            File fileSaved;
            Context mContext;

            saveWithStickers(Context context) {
                mContext = context;
            }

            @Override
            protected File doInBackground(Void... voids) {
                fileSaved = FileUtil.getNewFile(BaseActivity.this, "PHOTO EDITOR");
                if (fileSaved != null)
                    runOnUiThread(() -> stickerView.save(fileSaved, true));
                return fileSaved;
            }

            @SuppressLint("SetTextI18n")
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                Toast.makeText(mContext, "Saving ...", Toast.LENGTH_SHORT).show();
            }

            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);

        }
    }

To access to the context you need to pass it or use WeakRerefence to your parent activity

Jasurbek
  • 2,946
  • 3
  • 20
  • 37