Search code examples
delphidynamicuser-controlsfiremonkeydelphi-xe5

Get Active Form in FireMonkey Android Application


I'm stuck. How to get an active form in FireMonkey Android application? I have only a TComponent placed on this form, but it seems like it doesn't contain a reference to the root element.

I dynamically create a control (TToolBar) and want to add it to the top of the active form, when component is placed on it. The problem is:

  ToolBar := TToolBar.Create(Application);
  ToolBar.Align := TAlignLayout.alTop;
  ToolBar.Parent := ?;  // I don't know what parent to specify for this control

May be I should instantiate a new form and place the control on it?


Solution

    1. The right solution:

      ToolBar.Parent := Application.MainForm;
      (docs)

    2. This will work, but you will be unable to add childrens to the ToolBar:

      if Application.HasParent then
      ToolBar.Parent := Application.GetParentComponent as TFmxObject;
      (docs)

    3. This internal function should also do the trick in case if you know FormFamily:

      function Application.GetDeviceForm(const FormFamily: string): TCommonCustomForm; overload;