Is there any way to close Activity?

I’m running Android Activity made by Unity from another Activity written with Java.

The code like this will close the whole Application.

if(Input.GetKeyDown(KeyCode.Escape)){
    		Application.Quit();
    	}

Is there any way to close Activity when I hit back button?

Thanks.

2 Likes

forum.unity3d.com/threads/61851-How-to-Use-hardware(H-W)-MENU-KEY

you may find what you want here :slight_smile:

Hi kalos,

I read through the forum that you gave me.
However, I could only find out the way to handle the H/W keys.

I am searching a way to close an Activity by a program but not an Application.
I’m starting an Activity that made with Unity from another Android Activity.
So, I just want to close the unity activity not the entire application.
Application.Quit(); will close whole application.

Thanks anyway for your replay :slight_smile:

shinobu_siv, did you figure out a way to do this? I’m having the exact same problem. If they click back I want it to return to the activity that called it.

I haven’t got anything set up that would let me try this but you could try calling UnityPlayer.currentActivity.finish(); from inside the Java code.

Thanks Fenyx4, that is getting me closer. However, the whole activity stack is still closing. I actually see my calling activity flash on the screen briefly before it too is finished. I threw in a toast message on my onResume method in the calling activity and it does show briefly, so the unity activity is returning to the calling activity, but it then destroys that too. Any ideas why calling finish on the UnityPlayer would kill the whole activity stack?

Finally got the emulator and debugger working and I see this after the Unity activity finishes when it’s trying to restart my calling activity.

WARN/ActivityManager(71): Activity idle timeout for HistoryRecord{44e7ead0 com.XXXX.XXXX/.start}
WARN/ActivityManager(71): Activity destroy timeout for HistoryRecord{44fd7b68 com.XXXX.XXXX/.UnityActivity}

Any ideas whats going on? Why would the calling activity timeout? The UnityPlayerActivity sure doesn’t act like normal Android activities.

yaa its possible.inside keycode.backkey write System.exit(0).It wil close the activity.If facing some problem let me know what codes you write on keycode.back?

did you solve this???
the same happend to me i can see the last activity for less than a second and then home screen.

Thanks

I am having the same problem, I need to launch a Unity Activty from within my larger android app, end the unity activty when they hit a button, thus returning to the root activity. Everything runs fine, but It seems like somewhere in the Unity player destroy function it is forcing the entire app to close and not just the unity player activity.

I’m using java in eclipse, i guess it’s te same code cause it’s the android/java compiler that compiles it.

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK) //when the back button is pressed
{
finish(); //Close activity
return true; //onKeyDown is handled by user
}
return super.onKeyDown(keyCode, event);
}

How did you solved this? I’m struggling with the same issue here.
Thanks for your help!

I didnt fix it , the only option was to leave the Unity player activity stopped in the background, and resumes when needed. This puts extra memory constraints on the program and is something that Unity needs to adress.

Could you use FLAG_ACTIVITY_NEW_TASK in the Intent that you use to open the Unity activity? That might help.

I think most of you people (and most users and many other Android developers) have no idea how Android works.

It’s absolutely ok to minimize/put the current app in background. It won’t slow down the system at all. Apps that are in background typically don’t consume CPU. They are in sleep state. And yes, while they are still loaded in memory, this doesn’t slow the Android device at all.

If there is memory left, newly started apps and activities will use that free memory. If the device runs low on memory, the Android OS will release the apps in background to free the memory.

What you have to do is, to save any vital data when the app gets in background, because you don’t know if or when the app may be killed from the memory by the OS. So basically in “onPause” you must save your app informations and call it in “onRestart” (if the app was killed) or in onResume if it wasn’t.

That’s when you write traditional Android Apps in Java. Unity has similar ones: OnApplicationPause and OnApplicationFocus, which should be used to save the current game’s progress. Haven’t tested it yet, but it should work on Android too.

I’m also experiencing issues with this. We use Application.Quit() from withing the game, and I’ve also tried Unityplayer.currentActivity.finish().

The activity closes and I’m returned to the previous activity, but the problem comes in when attempting to reload the game activity. The newly created gaem activity crashes as it starts loading.

I did some further investigation and used DDMS to profile - revealing that there’s a GLThread that stays left behind after each game activity end, regardless of which way I end the activity.

Anyone have any insight regarding this…please…I’m at wit’s end here :confused:

Hi, i have the same problem, anybody knows how to solve it?

Any updates on this? It looks like this is my problem too.

Could you explain how to send the Unity player activity to the background? Is this done in Unity or in Eclipse?

Thanks in advance.

Any pointers on how to achieve this???