其他分享
首页 > 其他分享> > 如何从Unity Activity调用先前的Android Activity

如何从Unity Activity调用先前的Android Activity

作者:互联网

我的主要活动有一个启动UnityActivity的按钮.我需要完成UnityActivity并返回上一个活动.当按下返回按钮时,它将关闭整个应用程序.

我能做什么?谢谢!

编辑:

我使用高通增强现实(Unity Extension)的AR播放器.
我只有一个主要活动,可以从Qualcomm Augmented Reality启动AR播放器.

主要活动

public class Main extends Activity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public void onBtnStartClick(final View v) {
        Intent i= new Intent(this,ArPart.class);
        startActivity(i);
    }
}

AR玩家活动

public class ArPart extends QCARPlayerActivity {
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
             finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}

解决方法:

我发现使用覆盖的onBackPressed()最简单,但要这样做,还必须在Unity项目中进行更改:

void Update ()
{
    if (Input.GetKeyUp (KeyCode.Escape)) {

        AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); 
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity"); 
        jo.Call ("onBackPressed");
    }
}

然后,在您的活动课中:

@Override
public void onBackPressed() {

    // Handle the activity however you want,
    // what you do here will be executed when the back button is pressed
}

另外,请记住,要使此功能按预期工作,不能暂停UnityPlayer对象-处理后退按钮按下事件后应暂停它.

值得关注的是来自@Frank Nguyen的线程:Can’t pass back event from Unity to android library jar.

标签:unity3d,back-button,android
来源: https://codeday.me/bug/20191102/1988944.html