无法在Unity Activity中返回结果

时间:2018-04-06 12:47:08

标签: android unity3d startactivityforresult

我遇到了以下问题,

我有一个应用程序托管我的UnityApplication,该应用程序是通过MainApplication通过startActivityForResult调用的。 我在调用 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 App返回到我的主应用程序?

1 个答案:

答案 0 :(得分:1)

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