Search Unity

Is there any way to close Activity ?

Discussion in 'Android' started by shinobu_siv, Jan 25, 2011.

  1. shinobu_siv

    shinobu_siv

    Joined:
    Oct 18, 2010
    Posts:
    11
    I'm running Android Activity made by Unity from another Activity written with Java.

    The code like this will close the whole Application.
    Code (csharp):
    1.  
    2. if(Input.GetKeyDown(KeyCode.Escape)){
    3.             Application.Quit();
    4.         }
    5.  
    Is there any way to close Activity when I hit back button?

    Thanks.
     
    Abilash4285 and ted537 like this.
  2. kalos

    kalos

    Joined:
    Dec 11, 2010
    Posts:
    121
    forum.unity3d.com/threads/61851-How-to-Use-hardware(H-W)-MENU-KEY

    you may find what you want here :)
     
  3. shinobu_siv

    shinobu_siv

    Joined:
    Oct 18, 2010
    Posts:
    11
    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 :)
     
  4. kbradford

    kbradford

    Joined:
    Mar 22, 2011
    Posts:
    4
    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.
     
  5. Fenyx4

    Fenyx4

    Joined:
    Aug 5, 2010
    Posts:
    23
    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.
     
  6. kbradford

    kbradford

    Joined:
    Mar 22, 2011
    Posts:
    4
    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?
     
  7. kbradford

    kbradford

    Joined:
    Mar 22, 2011
    Posts:
    4
    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.
     
  8. Milan_cool

    Milan_cool

    Joined:
    Jan 24, 2011
    Posts:
    22
    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?
     
  9. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    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
     
  10. jdkflorida

    jdkflorida

    Joined:
    Apr 18, 2011
    Posts:
    3
    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.
     
  11. jrnvnjk

    jrnvnjk

    Joined:
    Nov 8, 2011
    Posts:
    1
    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);
    }
     
  12. r4ul90

    r4ul90

    Joined:
    Feb 7, 2012
    Posts:
    2
    How did you solved this? I'm struggling with the same issue here.
    Thanks for your help!
     
  13. jdkflorida

    jdkflorida

    Joined:
    Apr 18, 2011
    Posts:
    3
    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.
     
  14. tenfour

    tenfour

    Joined:
    Jan 3, 2012
    Posts:
    7
    Could you use FLAG_ACTIVITY_NEW_TASK in the Intent that you use to open the Unity activity? That might help.
     
  15. Tseng

    Tseng

    Joined:
    Nov 29, 2010
    Posts:
    1,217
    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.
     
  16. aphexyuri

    aphexyuri

    Joined:
    Oct 7, 2010
    Posts:
    4
    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 :/
     
  17. Angie

    Angie

    Joined:
    Mar 28, 2012
    Posts:
    2

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

    thomam

    Joined:
    Aug 21, 2012
    Posts:
    3
    Any updates on this? It looks like this is my problem too.
     
  19. art24

    art24

    Joined:
    Aug 28, 2012
    Posts:
    1
    Could you explain how to send the Unity player activity to the background? Is this done in Unity or in Eclipse?

    Thanks in advance.
     
  20. udaanparvaz

    udaanparvaz

    Joined:
    Jul 3, 2012
    Posts:
    4
    Any pointers on how to achieve this???
     
  21. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    I did something like this

    Called super.onDestroy(); in the public void onPause() method of the activity which extends the Unity activity... called onDestroy within some if condition... Might be helpful to you...But don't know whether that's a proper way to do it.
     
    Last edited: Oct 5, 2012
  22. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    For anyone having a similar issue...

    Later when testing, realized that calling super.onDestroy(); closes my app completely in some phones.

    So finally found a better solution.
    In the activity which extends the Unity activity (or QCARPlayerActivity/QCARPlayerProxyActivity ), when calling the next activity called it like startActivityForResult(Intent, int) instead of startActivity and added another method in that class onActivityResult(int, int, Intent). And called finish() inside this method.

    Now it seems to work fine and properly close the Unity instance.
     
  23. exoszajzbuk

    exoszajzbuk

    Joined:
    Apr 11, 2012
    Posts:
    10
    Hey madhur!

    I tried your solution with no luck, can you tell me, what could be wrong?

    Can you tell me, how to use startActivityForResult() with this configuration? In which Activity should I override onActivityResult()? The app starts with the native MenuActivity, which starts UnityActivity. From there I should be able to "close" the UnityActivity (return to menu) and then re-return to Unity. :)

    I assume I have now the same problem you had:
    1. If I finish() the UnityActivity, it explicitly kills the whole app (including the menu)
    2. If I simply start the MenuActivity when "returning" to menu I'm able te restart Unity, but since it's not completely closed, it refuses to display the splash screen => in my case it leads to 10-20 secons of black screen...

    I've already encountered this problem several times, and yours looks the first viable solution, so I'd be very happy if you could help me out.

    Thanks a lot in advance! :)
    exoszajzbuk

    -----

    I have the following C# script attached to a GameObject in Unity:

    Code (csharp):
    1.  
    2. ...
    3. private AndroidJavaClass jc;
    4. ...
    5. void Start () {
    6.     jc = new AndroidJavaClass("com.mycompany.MyPackageName.UnityActivity");
    7. }
    8. ...
    9. void OnGUI() {
    10.     if (menuButton.Button())
    11.     {
    12.         jc.CallStatic("menuClicked");
    13.     }
    14. }    
    15.  
    16.  
    -----

    My UnityActivity is the following:

    Code (csharp):
    1.  
    2. public class UnityActivity extends QCARPlayerActivity {
    3.     public static UnityActivity currentActivity;
    4.     ...
    5.  
    6.     public void onCreate(Bundle savedInstanceState)
    7.     {
    8.         super.onCreate(savedInstanceState);
    9.    
    10.         currentActivity = this;
    11.     }
    12.  
    13.     ...    
    14.  
    15.     public static void menuClicked()
    16.     {      
    17.         // go back to menu
    18.         Intent menu = new Intent(currentActivity.getApplicationContext(), MenuActivity.class);
    19.  
    20.         // ??? what to do here ???
    21.         //currentActivity.startActivityForResult(menu, 1);
    22.     }
    23. }
    24.  
    ---

    And finally my MenuActivity:

    Code (csharp):
    1.  
    2. public class MenuActivity extends Activity {
    3.  
    4.     @Override
    5.     public void onCreate(Bundle savedInstanceState) {
    6.         super.onCreate(savedInstanceState);
    7.         setContentView(R.layout.menu);
    8.     }
    9.    
    10.     ...
    11.  
    12.     public void unityClicked(View v)
    13.     {
    14.         // start unity activity with correct scene
    15.         Intent start = new Intent(getApplicationContext(), UnityActivity.class);
    16.         this.startActivity(start);
    17.     }
    18.    
    19.     ...
    20. }
    21.  
     
  24. madhur

    madhur

    Joined:
    May 16, 2012
    Posts:
    86
    Sorry for the very late reply(Just saw your question). You must have already fixed this by now. Anyway to answer your question,
    I think the the onActivityResult should come in the same class where you call startActivityForResult ie. UnityActivity class.
    That method will be something like this
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(resultCode == 1){
    this.finish();

    }
    }
     
  25. dttngan91

    dttngan91

    Joined:
    Nov 21, 2013
    Posts:
    79
    override onPause() in UnityPlayerActivity, call super.onDestroy() instead of super.onPause() is the solution for unexpectedly exit app when call finish() in unity.
     
  26. kingws

    kingws

    Joined:
    Sep 23, 2014
    Posts:
    7
    None of the recommended approaches to this thread seem to work. I've dug through the Unity documentation and don't see anything explaining why this happens. Calling finish(); from the unity activity should ONLY close the unity activity, not everything else up the stack!!. That's how standard Android activities work. I find it astonishing, given the cost of a unity license, that this hasn't been resolved.
     
  27. ddfire

    ddfire

    Joined:
    Dec 22, 2010
    Posts:
    53
    Actually this is not 100% accurate...
    Android can destroy any activity (except the one is the user using) to claim memory if needed.
    also maybe there is some tags in the manifest that can change this behavior
     
  28. kingws

    kingws

    Joined:
    Sep 23, 2014
    Posts:
    7
    Here's how I got it to work in my app:

    C#
    Code (CSharp):
    1. void Update()
    2. {
    3.     #if UNITY_ANDROID
    4.     if (Input.GetKeyDown (KeyCode.Escape)) {
    5.         AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    6.         AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
    7.         jo.Call ("closeActivity");
    8.     }
    9.     #endif
    10. }
    Calling Android Activity
    Code (JavaScript):
    1. Intent intent = new Intent(getApplicationContext(),
    2.                 UnityPlayerNativeActivity.class);
    3. intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    4. startActivity(intent);
    UnityPlayerNativeActivity
    Code (JavaScript):
    1. public void closeActivity() {
    2.     this.mUnityPlayer.quit();
    3.     UnityPlayer.currentActivity.finish();
    4. }
    The key was the call to this.mUnityPlayer.quit() before calling UnityPlayer.currentActivity.finish(). Can't say this is the best way to do it, but it's working for my purposes.
     
    RaphaelGM likes this.
  29. VickyDroid

    VickyDroid

    Joined:
    Jan 21, 2016
    Posts:
    1
    #Working Solution : To close the Android UnityActivity on BackPress use the below steps .

    Code (CSharp):
    1. void Update()
    2. {
    3.     #if UNITY_ANDROID
    4.     if (Input.GetKeyDown (KeyCode.Escape)) {
    5.        Application.Quit();
    6.     }
    7.     #endif
    8. }

    This will lead the Andriod Application to close the activity -> UnityPlayerNativeActivity extends NativeActivity .

    Code (Java):
    1. // Quit Unity
    2.     @Override
    3.     protected void onDestroy() {
    4.         mUnityPlayer.quit();
    5.         super.onDestroy();
    6.     }
    But at this stage the application itself gets closed , in-order to close the UnityActivty alone

    Just Use the below line in android Manifest for your UnityActivity

    Code (XML):
    1. <activity
    2. ...
    3. android:launchMode="singleTask"
    4. android:process=":UnityKillsMe">
    5. ...
    6. </activity>
    This procedure worked like charm for me .... :)
     
  30. najhi_1989

    najhi_1989

    Joined:
    Apr 21, 2017
    Posts:
    1
    I love u VickyDroid
     
  31. priyanka2017

    priyanka2017

    Joined:
    Oct 5, 2017
    Posts:
    2
    Thank u so much VickyDroid..this worked for me :)
     
  32. pixelworshipco

    pixelworshipco

    Joined:
    Sep 29, 2016
    Posts:
    6
    Perfect VickyDroid. Thank you
     
unityunity