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

Code before:
    public 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
                logger.w("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;
        }

        if (context instanceof ContextWrapper) {
            Context baseContext = ((ContextWrapper) context).getBaseContext();
            return getActivity(baseContext);
        }

        return null;
    }

Review comment (suggestion):
@copilot remove the first part of this `if` that uses `FragmentActivity`, I don't think we need it.

Here is Glide's code:
* https://github.com/bumptech/glide/blob/8a23b1e5db8c03330a898164df5c4fa7f90fc058/library/src/main/java/com/bumptech/glide/manager/RequestManagerRetriever.java#L234-L239

```java
  private static void assertNotDestroyed(@NonNull Activity activity) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) {
      throw new IllegalArgumentException("You cannot start a load for a destroyed activity");
    }
  }
```

Our minimum is already API 21, so I think we could just do:
```csharp
if (activity.isFinishing() || activity.isDestroyed()) {
    return true;
}
```

Code after:
            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
        // we don't want Layouts/images/Other controls to automatically toggle to Yes.
        // Unfortunately Android (AFAICT) doesn't have an obvious way of calculating what "Auto" will be interpreted as
        // iOS is kind of enough to indicate that anything inheriting from "UIControl" but the Android documentation
        // just says "Android uses heuristics to figure out what Auto will mean"
        // It seems like if we just toggle this back to "Auto" that everything just works.
        int importantForAccessibility = view.getImportantForAccessibility();
        view.setContentDescription(description);
        if (importantForAccessibility == View.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            view.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_AUTO);