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

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

×