How to return from Unity Player to previous running Android Activity?

I have the same problem as mentioned in this topic Integrating Unity with Eclipse: Back Button Click but the suggested solutions didn't work.

I have a Unity Player embedded in a Native Android Application. I can start the Unity Player without a problem but now I want to go back to my previous activity with the back button. I already tried Application.Quit from Unity side and finish() from android side, both closes the whole application. I also tried to start a new Activity from the Unity context but this doesn't work either, I think this is because I work with fragments. My Activity setup is as follows:

MainActivity where several fragments are embedded. Two of them contain Lists from which I can start a new Activity which shows me detail views of the List elements. The detail views are also fragments in which you can swipe left and right to previous/next element (similar to gmail app). From within the detail view, you can start Unity to have some augmentations with the qualcomm vuforia framework.

up to this point everything works, but I can not go back from the UnityPlayer to the details view.

Does anyone have any idea how to achieve this?

Thanks!

Answers


When you back press in unity activity the whole process will be killed. Therefore when you are starting a unity activity through android activity , start unity activity in different process. You can do this by declaring this in android manifest file of project.


You should add in AndroidManifest.xml next lines of code for UnityActivity.java:

    <activity
        android:name=".ui.activities.UnityPlayerActivity"
        android:label="@string/app_name"
        android:launchMode="singleTask"  <-- THIS
        android:process=":unityplayer"   <-- AND THIS
        ...    
    >
        ...
    </activity>

It's will run UnityActivity like another process and Unity command: Application.quit(); Will close only Unity Process.

Also if it will be not enough you can add those lines to activity that start UnityActivyty. But be careful, another package activity will most provide its own sharedPreferences and some other stuff.


I'm working on an Android project involving fragments Unity3d at the moment and it seems I've got a similar problem.

As far as I know, it's not fragments' fault - it's Unity that does something funny like intercept the back button press events. What I did was override the below methods to determine what's going on when a back button is pressed:

@Override
public void onBackPressed() {

    Log.e("Test", "onBackPressed here");
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    Log.e("Test", "onKeyDown here");

    if (keyCode == KeyEvent.KEYCODE_BACK) {

    Log.e("Test", "onKeyDown was back button");
    }

    return super.onKeyDown(keyCode, event);
}

The problem I discovered is that sometimes no log messages appear when I press the back button - i.e. it's not actually registered. At first I thought it had something to do with my calling UnityPlayer.pause() in the onResume() method but when I ran the project again this morning it turned out that the problem persists, AND it's intermittent.

If I come up with a solution I'll edit my post in this thread. I hope the explanation was at least of some help.


Edit:

Have a look at this link: Can't pass back event from Unity to android library jar. There's a solution there that you can implement in your Unity project to help with the back button press handling.

I did that and then carried out some more testing and it looks like the onBackPressed() method gets called all right when the UnityPlayer object is NOT paused. This leads me to believe that the UnityPlayer.pause() method should only be used when your app is going put in the background, not when you simply aren't needing the Unity model at the given time.


After adding the following to your Manifest file:

android:launchMode="singleTask"
    android:process=":unityplayer"

Also make sure you modify the following method in UnityPlayerActivity like this:

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

You can add your own code in onBackPressed().


// Just add below two line code In your Unity module manifast. under UnityPlayerActivity

        android:launchMode="singleTask"
        android:process=":unityplayer"

And Unity back button click event add this below code Application.quit();

Its working for me perfectly


Need Your Help