0

I'm using Delphi 10.1 Berlin.

Everything was OK under XE7.

Now when I start an activity, the result seems to be handled within a thread, and a synchronization/Queue would hang whole application. Since I need to display a TDialogServiceAsync.InputQuery, and if I don't sync, I get an exception telling me I must display my message from the main thread (that was working just perfectly under XE7)

here is the simplified code (tested so) that 'was' working :

Call :

FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(
  TMessageResultNotification, HandleActivityMessage);
intnt := TJIntent.JavaClass.init;
intnt.setAction(StringToJString('com.domain.mylib.MYCLASS'));
intnt.setPackage(StringToJString('com.domain.mylib'));
TAndroidHelper.Activity.startActivityForResult(intnt, REQUEST_CODE);

Handler :

HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
  // I tried TThread.Sync/Queue here... Hang
  if M is TMessageResultNotification then
  begin
    OnActivityResult(
      TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value
    );
  end;
end;


OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent);
var
  Values: array of String;
begin
  if (RequestCode = REQUEST_CODE) and (ResultCode = TJActivity.JavaClass.RESULT_OK) then
  begin
    //Either
    TDialogServiceAsync.ShowMessage('...');
    // Or
    SetLength(Values, 1);
    Values[0] := '';
    TDialogServiceAsync.InputQuery('something', ['info'], Values, Self.OnInputQuery2_Close);
  end;
end;

With this exception (original and translation...):

'Les messages doivent être affichés dans le thread principal de l'interface utilisateur'

'Messages must be displayed from user interface main thread'

Community
  • 1
  • 1
Darkendorf
  • 463
  • 11
  • 28
  • Thanks in advance for any help, this Berlin new behaviour is quite weird... – Darkendorf Sep 23 '16 at 09:16
  • `Thanks in advance for any help`. That we dont want. We want explicit thanks when you have been helped. – greenapps Sep 23 '16 at 09:42
  • Seems a bit curious... I have an app that uses this mechanism to respond to ZXing scanning a barcode. That had a `TDialogService.MessageDialog` call in a 'TThread.Queue` invocation within the message manager invoked routine. Changing it to `TDialogServiceAsync.MessageDialog` and re-running the message box still pops up readably and can be dismissed acceptably. My `TThread.Queue` is in the `TMainForm.OnActivityResult` method. – blong Sep 23 '16 at 20:54
  • But you are right inasmuch as Berlin produces an exception if you don't synchronize the message box when invoking it from what is in essence a Java callback - you must switch context to the FMX thread. – blong Sep 23 '16 at 20:56
  • I tried Sync/Queue(nil, proc) from both my HandleActivityMessage and OnActivityResult (around the whole proc content and just the TDialogServiceAsync call) but app always hang at the call... – Darkendorf Sep 26 '16 at 12:22
  • Most curious. Have you tried a simple app, just launching the ZXing barcode scanner, and doing the same in its callback? That was my test, which worked peachy. Maybe try the simple case, which is usually a sensible strategy for determining the location of an issue. – blong Sep 30 '16 at 21:24
  • So you're telling me that you noted no differences while moving to Delphi 10.1 Berlin from previous versions for this kind of code ? still my old code won't work anymore... – Darkendorf Oct 03 '16 at 13:23

1 Answers1

0

I copy-pasted again the original code from XE7, and it was still not working ("You must display messages from main thread")

I tested again TThread.Synchronize(nil, proc); at different places, and the application was always irremediably stunt.

Then tested again with TThread.Queue(nil, proc); at different places until it finally worked wrapping only the call to TDialogServiceAsync.

I finally found a solution, but this handler working like a thread that needs to be synchronized only for a message but that doesn't need synchronization for creating a new Activity and displaying it, is kind of disturbing !

If someone can give me an explanation on that point, feel free to add an answer that I will gladly accept as the solution.

Darkendorf
  • 463
  • 11
  • 28