Jump to content
iken

How to get the result of Activity.startActivityForResult in android

Recommended Posts

Open the photo album in TForm1.imagesClick:
var
  Intent: JIntent;

begin

  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_PICK);
  Intent.setType(StringToJString('image/*'));

  TAndroidHelper.Activity.getPackageManager.resolveActivity(Intent, 0);

  TAndroidHelper.Activity.startActivityForResult(Intent, 0, nil);

end

 

It also defines TForm1.onActivityResult(RequestCode, ResultCode: Integer; Data: JIntent);

to reading the result of the intent. But as a result, I can open the photo album and select the picture, but onActivityResult is not run after selecting the picture.

Share this post


Link to post
34 minutes ago, iken said:

But as a result, I can open the photo album and select the picture, but onActivityResult is not run after selecting the picture.

You need to do it this way, for example:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, System.Messaging,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure MessageResultNotificationMessageHandler(const Sender: TObject; const M: TMessage);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  Androidapi.Helpers, Androidapi.JNI.App, Androidapi.JNI.GraphicsContentViewText;

const
  cRequestCode = 1234; // Just a random number

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, MessageResultNotificationMessageHandler);
end;

destructor TForm1.Destroy;
begin
  TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, MessageResultNotificationMessageHandler);
  inherited;
end;

procedure TForm1.MessageResultNotificationMessageHandler(const Sender: TObject; const M: TMessage);
var
  LMessage: TMessageResultNotification;
begin
  LMessage := TMessageResultNotification(M);
  if LMessage.RequestCode = cRequestCode then
  begin
    if LMessage.ResultCode = TJActivity.JavaClass.RESULT_OK then
    begin
      // Here, handle the Intent that is the LMessage.Value property
    end;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  LIntent: JIntent;
begin
  LIntent := TJIntent.Create;
  LIntent.setAction(TJIntent.JavaClass.ACTION_PICK);
  LIntent.setType(StringToJString('image/*'));
  TAndroidHelper.Activity.startActivityForResult(LIntent, cRequestCode);
end;

end

 

  • Like 1

Share this post


Link to post

I managed to make it work, but at some points my app restarts.
The process is: 

 

MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);

AIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW);
      AIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK);
      AIntent.setData(TJNet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(AURI))));

     TAndroidHelper.Activity.startActivity(AIntent);

 

 

So far the second app opens correctly. But after I save it and return to my first app before entering the 'HandleActivityMessage' the app restarts. Any tips?

This doesn't happen all the time, but when it does I lose the intent return data.

 

 

 

 

Edited by Alisson

Share this post


Link to post
15 minutes ago, Alisson said:

TAndroidHelper.Activity.startActivity(AIntent);

...

before entering the 'HandleActivityMessage'...

You are not going to get a notification if you use startActivity(), you need to use startActivityForResult().

  • Like 1

Share this post


Link to post
6 hours ago, Alisson said:

I managed to make it work, but at some points my app restarts.

You appear to be trying to do something different from the OP.

6 hours ago, Alisson said:

But after I save it

Save what?

6 hours ago, Alisson said:

Any tips?

Yes: please describe what it is you are actually trying to achieve, preferably with a more complete example.

Share this post


Link to post

I have an application A that calls an application B with intent implicitly:

MainActivity.registerIntentAction(TJIntent.JavaClass.ACTION_VIEW);
TMessageManager.DefaultManager.SubscribeToMessage(TMessageReceivedNotification, HandleActivityMessage);

AIntent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_VIEW); 
AIntent.addFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); 
AIntent.setData(TJNet_Uri.JavaClass.parse(StringToJString(TIdURI.URLEncode(AURI)))); 

TAndroidHelper.Activity.startActivity(AIntent);


my Android manifest is configured as follows: 

<activity 
android:name="com.embarcadero.firemonkey.FMXNativeActivity" 
android:exported="true" 
android:label="%activityLabel%" 
android:configChanges="orientation|keyboard|keyboardHidden|screenSize|screenLayout|uiMode" 
android:launchMode="singleTop"> 
<!-- Tell NativeActivity the name of our .so --> 
<meta-data android:name="android.app.lib_name" android:value="%libNameValue%" /> 

<intent-filter> 
<action android:name="android.intent.action.MAIN" /> 
<category android:name="android.intent.category.LAUNCHER" /> 
</intent-filter> 
<intent-filter> 
<action android:name="android.intent.action.VIEW" /> 
<category android:name="android.intent.category.DEFAULT" /> 
<category android:name="android.intent.category.BROWSABLE" /> 
<date 
android:host="pay-response" 
android:scheme="retornopagamento/return_scheme" /> 
</intent-filter> 
</activity>

 

when I execute what I need in app B and close it.

It should go back to app A. But sometimes it restarts app A, instead of going back to where it left off.

I have a return string from App B that I collect in app A as follows:

procedure TfrmPagamentos.HandleActivityMessage(const Sender: TObject; const _AMensagem: TMessage);

begin
if _AMensagem is TMessageReceivedNotification then begin
if HandleIntentAction(TMessageReceivedNotification(_AMensagem).Value) then

Bill;

end else if (_AMensagem is TMessageResultNotification) then begin
if HandleIntentAction(TMessageReceivedNotification(_AMensagem).Value) then

Bill; // from the sequence

end;

end;

 

 

 

Share this post


Link to post
8 minutes ago, Alisson said:

<date 
android:host="pay-response" 
android:scheme="retornopagamento/return_scheme" /> 

No such element as "date" here - it should be: data. Is this a typo or what you actually have in your manifest?

10 minutes ago, Alisson said:

I have an application A that calls an application B with intent implicitly:

Are both of these your apps, or is application B a third-party app? I'm assuming the latter, since you do not appear to have included any code for it, and your example still is not complete enough to determine what the problem is. Regardless, I suggest you use a logcat viewer to determine exactly why app A is "restarting"

 

Share this post


Link to post
2 hours ago, Alisson said:

TAndroidHelper.Activity.startActivity(AIntent);

Again, appA MUST use startActivityForResult() in order to receive TMessageReceivedNotification with the result from appB.

  • Like 1

Share this post


Link to post
the "date" tag was a translation error. The correct one is: <D A T A 
I used startActivityForResult() but without success
Edited by Alisson

Share this post


Link to post

This is my logcat

 

04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2067)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at android.app.ContextImpl.startActivity(ContextImpl.java:994)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at br.com.stone.posandroid.acquirerapp.commons.deeplink.intent.payment.IntentDataSourcePaymentImpl.sendTransactionDeepLink(IntentDataSourcePaymentImpl.kt:117)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at br.com.stone.posandroid.acquirerapp.commons.deeplink.repository.DeepLinkRepositoryImpl$sendTransactionDeepLinkPayment$2.invokeSuspend(DeepLinkRepositoryImpl.kt:34)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677)
04-29 14:45:07.231  2206  3500 E b.c.s.p.a.c.d.i.p.IntentDataSourcePaymentImpl:         at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664)
04-29 14:45:07.337   955  1515 E POS_AT  : [SP_0] AT< 02000448330000037C
04-29 14:45:07.398   964  1522 E ActivityManager: Sending non-protected broadcast br.com.positivo.printer.onRealLength from system 2387:com.xcheng.printerservice/1000 pkg com.xcheng.printerservice
04-29 14:45:07.398   964  1522 E ActivityManager: java.lang.Throwable
04-29 14:45:07.398   964  1522 E ActivityManager:       at com.android.server.am.ActivityManagerService.checkBroadcastFromSystem(ActivityManagerService.java:16160)
04-29 14:45:07.398   964  1522 E ActivityManager:       at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:16856)
04-29 14:45:07.398   964  1522 E ActivityManager:       at com.android.server.am.ActivityManagerService.broadcastIntentLocked(ActivityManagerService.java:16177)
04-29 14:45:07.398   964  1522 E ActivityManager:       at com.android.server.am.ActivityManagerService.broadcastIntentWithFeature(ActivityManagerService.java:17024)
04-29 14:45:07.398   964  1522 E ActivityManager:       at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:2294)
04-29 14:45:07.398   964  1522 E ActivityManager:       at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2993)
04-29 14:45:07.398   964  1522 E ActivityManager:       at android.os.Binder.execTransactInternal(Binder.java:1154)
04-29 14:45:07.398   964  1522 E ActivityManager:       at android.os.Binder.execTransact(Binder.java:1123)
04-29 14:45:07.515   390   426 E UxUtility: notifyAppState error = NULL
04-29 14:45:08.188  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:45:08.192  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:45:08.731   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:11.134   964  1592 E TaskPersister: File error accessing recents directory (directory doesn't exist?).
04-29 14:45:11.744   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:14.762   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:20.801   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:23.821   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:29.860   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:32.878   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:35.898   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:38.917   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:41.934   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:44.951   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:50.987   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:54.006   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:45:57.025   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:00.048   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:03.068   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:06.087   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:09.108   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:11.967   390   426 E UxUtility: notifyAppState error = NULL
04-29 14:46:12.130   964  1067 E WifiVendorHal: getWifiLinkLayerStats_1_3_Internal(l.973) failed {.code = ERROR_NOT_SUPPORTED, .description = }
04-29 14:46:12.298   288   288 E lowmemorykiller: Fail to connect to socket duraSpeedMem. return code: -1
04-29 14:46:12.298   288   288 E lowmemorykiller: trigger duraSpeed: duraspeed_fd is lower than 0
04-29 14:46:13.128  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.129  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.212  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.213  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.403  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.404  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.492  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.492  2206  2206 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.532  2206  3757 E oid.acquirerap: Invalid ID 0x00000000.
04-29 14:46:13.533  2206  3757 E oid.acquirerap: Invalid ID 0x00000000.
logcat: Unexpected EOF!

This means that either the device shut down, logd crashed, or this instance of logcat was unable to read log
messages as quickly as they were being produced.

If you have enabled significant logging, look into using the -G option to increase log buffer sizes.

 

 

 

And this is link for integration: https://sdkandroid.stone.com.br/reference/pagamento-deeplink

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×