10

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!

Community
  • 1
  • 1
Solany
  • 101
  • 1
  • 3
  • Just a quick heads up, the Unity tag is reserved for Microsoft's Unity tool. Retagged as Unity3D. – Jerdak Sep 29 '13 at 16:57

5 Answers5

8

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.

8

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.

Dmitry Velychko
  • 1,505
  • 16
  • 14
  • Worked like a charm, now Unity doesn't kill previous activity/app. Thank you – Pelanes Feb 03 '17 at 13:15
  • I tried this but now my app is crashing saying that Firebase was not initialized on the unityplayer process. "Default FirebaseApp is not initialized in this process com.my.app:unityplayer. Make sure to call FirebaseApp.initializeApp(Context) first." I've never seen this before. Any hints? – kriztho Jan 23 '19 at 17:10
  • @kriztho with this code you initialoze UnityPlayerActivity in another process and whan you call some firebase code from UnityPlayerActivity it's says that firebase not initialized. So! You can try initialize FirebaseApp.initializeApp(this); in onCreate() method in UnityPlayerActivity. It's should work. – Dmitry Velychko Jan 28 '19 at 11:08
  • Unfortunately, it's still not working. I wonder if it's anything related to my app. I will try this on a test app to rule out everything to do with my app context being setup in a specific way. – kriztho Jan 30 '19 at 16:09
  • kriztho were you able to solve it bro?I am facing the exact same problem ! – Suhail Pappu Dec 24 '19 at 09:25
5

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().

Shesha Shankar
  • 131
  • 1
  • 3
  • 7
  • The process tag is making my app crash with the following error: "Default FirebaseApp is not initialized in this process com.my.app:unityplayer. Make sure to call FirebaseApp.initializeApp(Context) first." Any idea why or how to fix it? – kriztho Jan 24 '19 at 19:31
1

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.

Community
  • 1
  • 1
JakeP
  • 1,736
  • 4
  • 23
  • 31
0

// 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

Shohel Rana
  • 2,332
  • 19
  • 24