【发布时间】:2018-04-06 12:47:08
【问题描述】:
我遇到了以下问题,
我有一个托管我的UnityApplication 的应用程序,它是通过startActivityForResult 从我的MainApplication 调用的。
我在调用 mUnityPlayer.quit 之前设置了Result,但是当返回主应用程序时,数据意图始终为空。
onDestroy 部分中的代码:
@Override protected void onDestroy ()
{
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy called");
closeSession();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after closeSession");
//finish();
setResult(resultCode,resultData);
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: Result set 1");
UnityPlayer.currentActivity.setResult(resultCode,resultData);
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: Result set 2");
this.mUnityPlayer.quit();
super.onDestroy();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after super.onDestroy");
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy: after mUnityPlayer.quite");
UnityPlayer.currentActivity.finish();
Log.d(this.getClass().getSimpleName(),"UnityApp.onDestroy after finish");
}
调用活动代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CALL_EXTERN_APP) {
Log.d(this.getClass().getSimpleName(), "ListFiles:");
txtHelloWorld.setText("data==null");
if(data==null) return;
txtHelloWorld.setText("no Filename");
if(!data.hasExtra(EXTRA_SESSION_FILENAME)) return;
txtHelloWorld.setText("no Filecontent");
if(!data.hasExtra(EXTRA_SESSION_FILECONTENT))return;
txtHelloWorld.setText("Successful");
if (resultCode == 0 && callSuccess) {
String filename = data.getStringExtra(EXTRA_SESSION_FILENAME);
String[] filecontent = data.getStringArrayExtra(EXTRA_SESSION_FILECONTENT);
File file = new File(getFilesDir(),filename);
Log.d(this.getClass().getSimpleName(), "onActivityResult:" + filecontent.length + " Lines to write to " + filename);
try {
if(!file.exists())
file.createNewFile();
FileOutputStream fout = new FileOutputStream(file);
for (int l = 0; l < filecontent.length; l++) {
fout.write(filecontent[l].getBytes());
Log.d(this.getClass().getSimpleName(), "onActivityResult:" + "Line " + (l + 1) + ": " + filecontent[l]);
}
fout.close();
Log.d(this.getClass().getSimpleName(), "onActivityResult: Write to " + filename + " done");
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
我做错了吗?或者有没有更好的解决方案将数据从 Extern Unity 应用程序返回到我的主应用程序?
【问题讨论】:
-
执行此代码
onDestroy()不是一个好主意。可以尝试将onDestory()中的代码移动到这个activity完成任务的地方吗? -
你指的是代码的哪一部分?
-
您必须在某处调用
finish(),因此调用了onDestroy()。可以先执行代码再执行onFinish() -
谢谢
-
太棒了!我将其更新为答案
标签: android unity3d startactivityforresult