We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hi,
I've recently sketched up my first Android app which was surprisingly easy. But now I'm fighting with the little details. E.g. implementing dialogs for my apps preferences etc.. Especially I would like to override the default behaviour of the phones 'back'-button. I've implemented a button in my app that creates a KetaiList that's supposed to work as an options-menu. However, when this menu is presented the 'back'-button should rather bring the default-view of my app back to front than quit the app completely. I've searched within the Android documentation and found a few hints (I guess what I want to achieve is a very common demand). Especially this seemed to be promising: http://android-developers.blogspot.co.at/2009/12/back-and-other-hard-keys-three-stories.html. But I wasn't able to accomplish what I wanted... So far I've tried a couple of different solutions in my app:
void onBackPressed() {
println("back pressed");
switch(backKeyState) {
case 1:
println("go back");
moveTaskToBack(true);
break;
}
return;
}
... that didn't do the trick. Another strategy was to use the keyPressed handler in combination with a variable that holds the state to which the program should go upon tapping the 'back'-button:
void keyPressed()
{
if (key == CODED) {
if(keyCode == BACK) {
println("go back");
switch(backKeyState) {
case 0:
// quit program
println("quit");
break;
case 1:
// close currently open dialog
println("go to main.");
moveTaskToBack(true);
break;
}
}
}
}
... that didn't work either (though it prints correctly). Another thing that I tried was to replicate the functionality hidden behind in a KetaiList:
// preferencesList holds my KetaiList
((android.view.ViewGroup)preferencesList.getParent()).removeView(preferencesList);
... didn't work either.
Can anyone give me a hint on how to do this properly?
thanks in advance
Answers
Theoretically, overriding
onBackPressed()
should be the correct solution. I believe that something like this should work:However, it appears that there may be a Processing bug related to the back button and the
draw()
function. The above solution may work for you, but it may not work as well.Oh! Thanks. I'll try again after my phone's regarged... maybe it's related to kitkat as well. My app doesn't work in the emulator, so, for now I'm somehow dependent on my physical phone.
Just tried your code but unfortunately no success. However, I had a look at the bug-report on Github. I tried the minimal example (once with a
draw()
function, once without -- I can confirm the bug. It works if I leave out thedraw()
(which isn't too helpful in my case...).sorry for posting my reply 3 times... I was confused as I got an error-message but I wasn't redirected...