Repository: dotnet/maui
File: src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/PlatformInterop.java (Java)

Code before:
public class PlatformInterop {

    /**
     * Checks if the given context is destroyed.
     * This is similar to the C# IsDestroyed extension method in ContextExtensions.cs
     * @param context The context to check
     * @return true if the context is destroyed, false otherwise
     */
    static boolean isContextDestroyed(Context context) {
        Activity activity = getActivity(context);
        if (activity instanceof FragmentActivity) {
            FragmentActivity fragmentActivity = (FragmentActivity) activity;
            
            // Check if activity is finishing or destroyed
            if (fragmentActivity.isFinishing() || fragmentActivity.isDestroyed()) {
                return true;
            }

            // Check lifecycle state
            try {
                if (fragmentActivity.getLifecycle().getCurrentState() == Lifecycle.State.DESTROYED) {
                    return true;
                }
            } catch (Exception e) {
                // Log warning if there's an exception getting the lifecycle state, consider it destroyed
                android.util.Log.w("PlatformInterop", "Exception checking lifecycle state, assuming destroyed", e);
                return true;
            }
        } else if (activity != null) {
            // For regular Activity, check if finishing or destroyed
            if (activity.isFinishing() || activity.isDestroyed()) {
                return true;
            }
        }

        return false;
    }

    /**
     * Gets the Activity from a Context, similar to the C# GetActivity extension method.
     * @param context The context to get the activity from
     * @return The activity if found, null otherwise
     */
    static Activity getActivity(Context context) {
        if (context == null) {
            return null;
        }

        if (context instanceof Activity) {
            return (Activity) context;
        }

Review comment (suggestion):
This should use:
* https://github.com/dotnet/maui/blob/main/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/PlatformLogger.java

Check here for an example:
* https://github.com/dotnet/maui/blob/239fb5ffb3db1f530f57e1ab9a284939e290cfbd/src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/glide/font/FontModelResourceDecoder.java#L21

Code after:
        
        // If the view isn't currently in the layout process, then we simply request
        // that layout happen next time around
        if (!view.isInLayout())	{
            view.requestLayout();
            return;
        }
        
        /* 
            Something is requesting layout while the view is already in the middle of a layout pass. This is most 
            likely because a layout-affecting property has been data bound to another layout-affecting property, e.g. 
            binding the width of a child control to the ActualWidth of its parent.
            
            If we simply call `requestLayout()` again right now, it will set a flag which will be cleared at the end 
            of the current layout pass, and the view will not be arranged with the updated values.

            Instead, we post the layout request to the UI thread's queue, ensuring that it will happen after the current
            layout pass has finished. Layout will happen again with the updated values.
        */

        Runnable runnable = () -> { 
            if (!view.isInLayout())	{
                view.requestLayout();
            }
        };
        
        view.post(runnable);
    }

    public static void removeFromParent(View view) {
        ViewParent parent = view.getParent();
        if (parent == null)
            return;
        ((ViewGroup) parent).removeView(view);
    }

    public static void setPivotXIfNeeded(View view, float pivotX) {
        if (view.getPivotX() != pivotX)
            view.setPivotX(pivotX);
    }

    public static void setPivotYIfNeeded(View view, float pivotY) {
        if (view.getPivotY() != pivotY)
            view.setPivotY(pivotY);
    }

    public static void setContentDescriptionForAutomationId(View view, String description) {
        view = getSemanticPlatformElement(view);

        // Android automatically sets ImportantForAccessibility to "Yes" when you set the ContentDescription.
        // We are only setting the ContentDescription here for Automation testing purposes so