英文:
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:= '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;
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:
> cant perform this action after onSaveInstanceState
this is my AndroidManifest, where to use explicit intent by scheme
{<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>}
</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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论