不能 return 导致 Unity Activity

Can't return result in Unity Activity

我遇到了以下问题,

我有一个应用程序托管我的 UnityApplication,它是通过 startActivityForResult 从我的 MainApplication 调用的。 我在调用 mUnityPlayer.quit 之前设置了 Result,但是当 returning 到主应用程序时,数据意图始终为 null。

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");
   }

调用代码Activity:

@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();
                        }
                }
            }
        }

我是不是做错了什么?或者对于 return 从 Extern Unity 应用程序到我的主应用程序的数据有更好的解决方案吗?

在 onDestroy() 中执行此代码不是一个好主意。您应该将代码移动到您调用 finish() 的地方。这将确保您的代码可靠地工作并确保 onActivityResult() 被正确调用。