Madhan
Madhan

Reputation: 359

Fragment not associated with fragment manager exception in getParentFragmentManager

I have a fragment in my Photo app. When the user edits photo,

  1. I start an AsyncTask to compress the image in the background which will return compressed image bytes.
  2. In postExecute() i call an editComplete method which will update my data model with compressed image bytes
  3. 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?

Upvotes: 4

Views: 11916

Answers (1)

Madhan
Madhan

Reputation: 359

After further reading Android docs on Fragment Manager and this answer Fragment MyFragment not attached to Activity, i believe that isAdded() check is the way to confirm whether the fragment is associated with the activity through fragmentManager. It was confusing looking at the definition of the method at first,

/**
         * Return true if the fragment is currently added to its activity.
         */
        final public boolean isAdded() {
            return mHost != null && mAdded;
} 

Because it says that whether the host is not null and if the fragment is added. I think the key is mAdded which tells us whether the fragment is added to fragment Manager. So i updated my popstack logic with a check,

    protected void onEditImageComplete(@Nullable byte[] editedBitmapData)
    {
        if (editedBitmapData != null)
            photoModel.editedBitmapData = editedBitmapData;
        if (isAdded())
            getParentFragmentManager().popBackStack();
    } ```

Upvotes: 5

Related Questions