Author Topic: Android Back Button Pause/Resume  (Read 7801 times)

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Android Back Button Pause/Resume
« on: 2013-Sep-10 »
Hi everyone,

I am currently trying to figure out how to make my Android application pause when I use the back button or home button on my phone.

I tried to find some info on the forum, but without much success.

It is the same problem described by Ampus in this thread: http://www.glbasic.com/forum/index.php?topic=6788.0

Now, is there a solution for this problem yet? What do I have to do in order to make my application PAUSE when i click the back button, and not QUIT?

I;m pretty sure the problem has been asked and will be asked again, so I guess a step-by-step instruction would be fantastic.

Best,
P.
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline erico

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4450
    • View Profile
    • FUED
Re: Android Back Button Pause/Resume
« Reply #1 on: 2013-Sep-10 »
Good question, mind if I add a little extra?
It would also be nice to know how to disable the back button completely. My controls are a bit close to it and touching it by mistake promptly exits the app.

Offline r0ber7

  • Prof. Inline
  • *****
  • Posts: 552
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #2 on: 2013-Sep-10 »
I would take this approach (don't know if it works but you could try it).

Using this to implement the java call below: http://www.glbasic.com/forum/index.php?topic=8621.msg73049#msg73049

Code: (java) [Select]
    // Back key catching
    @Override
    public void onBackPressed() {
// do nothing
}

onBackPressed has the standard behaviour of quitting the app (not just in GLBasic), the above code overrides that. I encountered this during my work on a (Java) Android project. I haven't tested this from a GLBasic app, but it works in "normal" Android apps.
« Last Edit: 2013-Sep-10 by r0ber7 »

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4025
    • View Profile
    • Space Fractal
Re: Android Back Button Pause/Resume
« Reply #3 on: 2013-Sep-10 »
1. First update glbasic to SDK 4.0 (not sure its required by this issue throught, my projects and calls is now allways uses that SDK instead of SDK 2.2):
http://www.glbasic.com/forum/index.php?topic=9341.0

Sadly the above post require you have 100+ post, so IAN, its time to move this to a none bonus section. Im thinks Code Snippets is the best forum?

2. Uses ANDROIDEXTRAS newest version by FiveSprite and additions by me (inapp shos for OUYA, GameController supports, even for the menu button):
http://www.glbasic.com/forum/index.php?topic=9166.0

There is included a eailer ANDROIDEXTRAS  version in the SDK upgrade as well, so its can been tested trought.

with above, you can also fix the dreadfull orientation issue on some devices, if "sensor" not used.

3. Today im doing do a new update today, so SDLActivity.java can been detected, so the user is on the menu, then back button should quit the app (which is a NORMAL behaiver on Android). If the user is on the game, then back buttons would do nothing. So to pause the game, you should uses the MENU button instead, which is more correct way. That can allready been checked with ANDROIDEXTRAS (using the OUYA game controller code, dispite menu can been detected without using OUYA).

PS. Etc you could also tryout ANDROIDEXTRAS with the currect glbasic SDK (which uses v2.2), howover the manifest might require changes.
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #4 on: 2013-Sep-10 »
Hi spacefractal,

thank you for the reply.

Since I don't have 100+ posts already, I can't download the whole Android folder from the "Bonus" section (whatever THAT is supposed to be).

Now I am getting the exact same error as Marmor in this thread:

http://www.glbasic.com/forum/index.php?topic=9379

What can I do? Could you please just upload the Android folder to some download site? I really don't see the need for putting it in a "bonus" section.

Thank you in advance!

ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4025
    • View Profile
    • Space Fractal
Re: Android Back Button Pause/Resume
« Reply #5 on: 2013-Sep-10 »
im ask Ian to move the post to the Inline/3rd Party forum (have pm him). Im hope a day Gernot implement that in the next beta, since its diddent breaks Android 2.3 compatible at all, even using SDK 4.0 (but its might breaks 2.2 compatible, but here im dont care, due too few users that uses that).

its was the eventuelly traffic limit that was the reason im posted its in the limited forum, but not sure its a reason anymore.
« Last Edit: 2013-Sep-10 by spacefractal »
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #6 on: 2013-Sep-10 »
Ok, thank you very much!  :)
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4025
    • View Profile
    • Space Fractal
Re: Android Back Button Pause/Resume
« Reply #7 on: 2013-Sep-10 »
the post have now been moved to the Code Snippets:
http://www.glbasic.com/forum/index.php?topic=9341.0

im have still not updated ANDROIDEXTRAS using the menu thing, but things its easier to workaround the back button by lookin in public boolean onKeyDown(int keyCode, KeyEvent event) in SDLActivity.java

Here you can do this code to prevent the app closing down:

Code: (glbasic) [Select]
if (keyCode == KeyEvent.KEYCODE_BACK)
{ if (event.isAltPressed()) { return false; } // prevent quit if circle is pressed on Xperia Play
return false;
}
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #8 on: 2013-Sep-10 »
Changed that, doesn't make any difference. (I'm on Android 2.2.2 with my phone btw, so not exactly what one might call up to date. Maybe it's a compatibility issue?)

The problem is that I want the app to keep running in paused mode in the background. What it does here is just close and quit completely, so exiting and re-opening the app will always completely re-start the app. What I want to achieve is to be ablte to make use of the GLB_ON_PAUSE and GLB_ON_RESUME Subs (as far as I have understood, I just have to add these 2 subs to my source code).

There is a software called Basic4Android out there that handles the task almost the same (with an on_pause and on_resume routine). It would be so great to get this behavior working with GLBasic!

Best
P.
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4025
    • View Profile
    • Space Fractal
Re: Android Back Button Pause/Resume
« Reply #9 on: 2013-Sep-10 »
Im uses the onKeyDown(int keyCode, KeyEvent event) and public boolean onKeyUp(int keyCode, KeyEvent event) events for all manager all key presses, which works nice for my devices. Im thinks glbasic v10 did use a different api, but its did not works very well, why im changed it to use those.

Also im dosent have test it on Android 2.2 anymore, due its very low marketshare.

PS. Have you also tried its with glbasic 11.414 Beta with the new sdk? The upgrade was suitable to use with that beta, not Glbasic 10.
« Last Edit: 2013-Sep-10 by spacefractal »
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #10 on: 2013-Sep-10 »
Im uses the onKeyDown(int keyCode, KeyEvent event) and public boolean onKeyUp(int keyCode, KeyEvent event) events for all manager all key presses, which works nice for my devices. Im thinks glbasic v10 did use a different api, but its did not works very well, why im changed it to use those.

Also im dosent have test it on Android 2.2 anymore, due its very low marketshare.

PS. Have you also tried its with glbasic 11.414 Beta with the new sdk? The upgrade was suitable to use with that beta, not Glbasic 10.

Could you perhaps send me a more detailed instruction on how to use onKeyDown and OnKeyUp?

And also: Where fo i get the 11.414 Beta? Web Update doesn't tell me that there is v11 available - i only get informed about 10.283.

Thank you!
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #11 on: 2013-Sep-10 »
Ok, found the Beta and installed it.

It's getting better, but it's still not perfect.


When I close the App with the Home Button, it returns to the app when I retstart it. That seems to be OK as far as I can tell.

But when I close the App with the Backbutton, it will crash on re-opening and has to be closed/killed manually. That's bad.

I tested on Android 2.2.2 and 4.1.2 - same symptoms.

In my SDLActivity.java is the following code:

Code: (glbasic) [Select]
if(keyCode==KeyEvent.KEYCODE_BACK)
{
if(!event.isAltPressed()) // some handle alt+back as "circle"
{
Log.v("SDL", "keycode_back w/o alt-key -> finish?");
// mActivity.glbasicOnPause(1);
// SDLActivity.finish();
// System.exit(0);
return super.onKeyDown(keyCode, event);
}
}

Is this correct? Or do I have to uncomment the glbasicOnPause(1)?

Thank you!
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline Poetronic

  • Mr. Drawsprite
  • **
  • Posts: 98
    • View Profile
Re: Android Back Button Pause/Resume
« Reply #12 on: 2013-Sep-10 »
Update:

Ok, when I uncomment this line:

Code: (glbasic) [Select]
// System.exit(0);
The App is closed as expected when pressing the BACK button and has to be restarted. HOME Button sets the game on Pause (runnnig in the background). I use

Code: (glbasic) [Select]
AUTOPAUSE TRUE
ALLOWESCAPE FALSE

Seems like a working solution for now!  :)

Best
P.
« Last Edit: 2013-Sep-11 by Poetronic »
ILI-Blocks, my first game ever - please check it out! http://www.glbasic.com/forum/index.php?topic=8654.0

Offline spacefractal

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 4025
    • View Profile
    • Space Fractal
Re: Android Back Button Pause/Resume
« Reply #13 on: 2013-Sep-11 »
If you want prevent back button to do nothing, return false;instead of return super.onKeyDown(keyCode, event).

I'm look on a better issue later, so ouya can track the back key to the game controller super (etc so you can close app it's self when it's in menu).
Genius.Greedy Mouse - Karma Miwa - Spot Race - CatchOut - PowerUp Elevation and The beagle Jam.