Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 562
Solany Avatar asked Sep 29 '13 16:09

Solany


People also ask

How do I export from Unity to Android?

Export to Android Studio In Unity open File | Build Settings... , check Google Android Project and click Export. Open Android Studio and click Import Project (Eclipse ADT, Gradle, etc.) Within the file selection dialog, navigate to the folder where you exported the project and select the folder named after your app.

How does Unity run on Android?

When Unity builds an Android app, it includes a . NET bytecode interpreter in native code, based on Mono. When you run the app, the interpreter is run to execute the bytecodes. That's how it can run on Android.

Does Unity export to mobile?

For Unity to be able to export your game as an APK, you need to install the Android Build Support module on the Unity Editor that you are using to build your project. Under the Installs tab of Unity Hub, if you find the circled icon below your editor version, that means that Android Build Support is installed for it.


2 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.

like image 102
Parikshit Singh Shekhawat Avatar answered Sep 28 '22 08:09

Parikshit Singh Shekhawat


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.

like image 31
Dmitry Velychko Avatar answered Sep 28 '22 09:09

Dmitry Velychko