如何从Unity Activity调用先前的Android Activity [英] How to call previous Android Activity from Unity Activity

查看:106
本文介绍了如何从Unity Activity调用先前的Android Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My main activity has a button which launches UnityActivity. I need to finish the UnityActivity and return to previous activity. When pressing the back button it closes the whole application.

我该怎么办?谢谢!

我使用的是高通增强现实(Unity Extension)的AR播放器. 我只有一项主要活动是从高通增强现实技术启动AR播放器.

I use the AR player from Qualcomm Augmented Reality (Unity Extension). I have only one main activity which I start the AR player from Qualcomm Augmented Reality.

主要活动

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项目中进行更改:

I found it easiest to use the overridden onBackPressed() but to do that you also have to make a change in your Unity project:

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

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

然后,在您的活动课中:

Then, in your activity class:

@Override
public void onBackPressed() {

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

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

Also, remember that for this to work as expected, the UnityPlayer object cannot be paused - you should pause it after you've handled the back button press event.

信用从该线程转到@Frank Nguyen:无法将事件从Unity传递回android库jar .

Credit goes to @Frank Nguyen from this thread: Can't pass back event from Unity to android library jar.

这篇关于如何从Unity Activity调用先前的Android Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆