How to make Delphi Android app run in the background and then be maximized without being restarted?

huangapple go评论61阅读模式
英文:

How to make Delphi Android app run in the background and then be maximized without being restarted?

问题

我有一个用Delphi编写的Android应用程序。

在启动应用程序并通过Intent初始化另一个应用程序后,关闭第二个应用程序并返回到第一个应用程序时,第一个应用程序会重新启动。

注意:我看到了关于onSaveState事件的一些内容,但我需要我的应用程序不重新启动,因为我在其中使用了第二个应用程序的返回数据。

我通过Intent执行了第二个应用程序的初始化,当关闭第二个应用程序时,它应该返回到我打开第二个应用程序时可以操作Intent信息的第一个应用程序,但它重新启动了第一个应用程序。

更新:
--//--//--//--//--//--//
这是我的代码:

procedure TForm1.btnEnviarTransacaoClick(Sender: TObject);
begin
   try
      var AURI: string := 'payment-app://pay?return_scheme=retorno&amount=9999&editable_amount=0&transaction_type=credit';
      var AIntent: JIntent := TJIntent.Create;
      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.startActivityForResult(AIntent, 0);
      Timer1.Enabled := True;
   except
      on E: Exception do begin
         TThread.Synchronize(nil, procedure
         begin
            lOG.D(E.Message);
         end);
      end;
   end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   Timer1.Enabled := False;
   var ADLg := TForm2.Create(Self);
   ADLg.Show;
end;

注意:在启动活动后,我触发一个4秒的计时器来创建一个新的窗体并显示它,但在完成第二个应用程序并返回到主屏幕应用程序时,主屏幕应用程序重新启动。有时在调试时我设法捕获了异常:

无法在onSaveInstanceState之后执行此操作

这是我的AndroidManifest,其中使用了scheme的显式Intent:

<activity
    android:name=".PaymentActivity"
    android:launchMode="singleTop"
    android:exported="true">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:host="pay-response"
            android:scheme="retorno" />
    </intent-filter>
</activity>
英文:

I have an Android app written in Delphi.

When launching the app and initializing another app by Intent, after closing the second app and going back to the first app, the first app is restarted.

Note: I saw that there is something about the onSaveState event, but I need my app not to restart because I use the return data from the second app in it.

I performed the initialization of the second app by Intent and when closing the second app, it should go back to the first one at a point where I can manipulate the Intent information where I opened the second app, but it is restarting the first app.

UPDATE:
--//--//--//--//--//--//
Here is my code:


procedure TForm1.btnEnviarTransacaoClick(Sender: TObject);
begin
   try
      var AURI:string:= &#39;payment-app://pay?return_scheme=retorno&amp;amount=9999&amp;editable_amount=0&amp;transaction_type=credit&#39;;
      var AIntent: JIntent := TJIntent.Create;
      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.startActivityForResult(AIntent, 0);
      Timer1.Enabled:= True;
   except
      on E: Exception do begin
         TThread.Synchronize(nil, procedure
         begin
            lOG.D(E.Message);
         end);
      end;
   end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   Timer1.Enabled:= False;
   var ADLg:= TForm2.Create(Self);
   ADLg.Show;
end;

note: after start activity , I trigger a 4 second timer to create a new form and show it, but after finishing the second app and returning to the home app, the home app restarts. Sometimes debugging I managed to catch the exception:

&gt; cant perform this action after onSaveInstanceState

this is my AndroidManifest, where to use explicit intent by scheme

{&lt;activity
				android:name=&quot;.PaymentActivity&quot;
				android:launchMode=&quot;singleTop&quot;
				android:exported=&quot;true&quot;&gt;
			&lt;intent-filter&gt;
				&lt;action android:name=&quot;android.intent.action.VIEW&quot; /&gt;
				&lt;category android:name=&quot;android.intent.category.DEFAULT&quot; /&gt;
				&lt;category android:name=&quot;android.intent.category.BROWSABLE&quot; /&gt;
				&lt;data
					android:host=&quot;pay-response&quot;
					android:scheme=&quot;retorno&quot; /&gt;
			&lt;/intent-filter&gt;
		&lt;/activity&gt;}

</details>


# 答案1
**得分**: 1

我找到了问题:

在我的 AndroidManifest 中,主活动使用了 launchmode: SingleTask,只需将其更改为 singletop 就解决了问题。

<details>
<summary>英文:</summary>

I found the problem:

in my AndroidManifest, the main activity was using launchmode: SingleTask, just change it to singletop that solved the problem.

</details>



huangapple
  • 本文由 发表于 2023年7月28日 03:13:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76782776.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定