I have a fragment in my Photo app. When the user edits photo,
- I start an AsyncTask to compress the image in the background which will return compressed image bytes.
- In postExecute() i call an editComplete method which will update my data model with compressed image bytes
- Once it is done, i call getParentFragmentManager to popBackStack to remove the edit mode to gallery mode
Here while calling getParentFragmentManager() i am getting IllegalStateException: "Fragment " + this + " not associated with a fragment manager."
My Fragment Async task:
protected class CompressBitmapImageTask extends AsyncTask<Void, Void, byte[]>
{
private Bitmap editedImageBitmap;
private BitmapDownscale bitmapDownscale;
CompressBitmapImageTask(Bitmap editedImageBitmap, BitmapDownscale bitmapDownscale)
{
this.editedImageBitmap = editedImageBitmap;
this.bitmapDownscale = bitmapDownscale;
}
@Override
protected byte[] doInBackground(Void... params)
{
BitmapDownscale.BitmapDownscaleResult result = bitmapDownscale.downscaleFromBitmap(editedBitmap, true);
return result.bitmapBytes;
}
@Override
protected void onPostExecute(byte[] bytes)
{
onEditImageComplete(bytes);
}
}
protected void onEditImageComplete(@Nullable byte[] editedBitmapData)
{
if (editedBitmapData != null)
photoModel.editedBitmapData = editedBitmapData;
getParentFragmentManager().popBackStack();
}
I am getting exception when calling getParentFragmentManager(). I referred a related post, Fragment MyFragment not attached to Activity
But that is related to fragment not being associated with activity. So i am not sure if adding isAdded()
would solve the problem in my case.
Ideally, i need to make sure that fragmentManager is not null while i try to pop backStack(). Only method that does is isStateSaved() in androidx.fragment.app.Fragment but i don't think that is an appropriate method.
Can somebody point me in right direction?