How to handle android HOME button using libgdx - android

i`m writing game for android using libgdx.
And have problem that game controls are near android buttons HOME and BACK.
There is easy way to handle BACK button, but i havent found something for HOME button. All i want - when user accidentally presses HOME button - to handle it and popup dialog if user want to leave game but not to minimize it at once.

You can't handle Home button (nor Lock Screen).
Reference: How to catch Home/Lock Screen button

For anyone who might stumble on this, a plausible solution would be to implement the onPause method in your android launcher
#Override
protected void onPause(){
//do whatever you need to on pause
super.onPause();
}

Related

Block Home button,back button

I want to block home button,back button and minimize button in navigation bar. Is it possible to block this button in android version 4.4.2?
Normally you're not supposed to block the usage of the Home button. Users should always be able to exit your app through the use of the home button. There is no api to disable this button.
1) If it is really necessary to block this button, you can make a custom OS with that functionality. There are most likely better solutions though.
2) You could register your app as a launcher app. This way you will know when the user presses the Home button and can act accordingly.
You can override the functionality of the back button.
Override onBackPressed in your activity.
I don't know what you mean with the "minimize button".
#Override
public void onBackPressed()
{
super.onBackPressed();
}
in this code remove super.onBackPressed(); line.
To remove back button.

Disable Back Button (onBackPressed)

I want to disable back button from closing the app.
How can i disable back button?
You can override the onBackPressed() method and determine what happens in that method. Like this:
#Override
public void onBackPressed() {
// do nothing because you don't want them to leave when it's pressed
}
Just add that method to your activity class and you're good to go.
However, this is bad app design. What you would most likely want to do is make a dialog pop up that asks them if they are sure they want to leave. You would add the dialog code inside that method so that when the back button is pressed, the dialog pops up.
Generally, that's not a good idea. Users hate to feel "trapped" in your app.
Many users are able to start apps "on top of" other apps. When they hit "back" they may expect your app to stop, and the app they were in previously to appear. This is different from "home" where they expect all apps to go to the background.
Users familiar and comfortable with this functionality will not like it if you change "back" - although you may give them options like "press back again to exit" as some apps do. It depends on your particular situation.
So if you are in need of it, here is a good reference:
Android - How To Override the "Back" button so it doesn't Finish() my Activity?

how we handle onBackPressed in an android Service

I develop an application of cracking screen i run an android service to showing crack screen on home screen now i want to repair when user pressed back button on five times while our service is running on top and show crack screen how can i handle back button within a service please help me for this how we handle back button in a services like this app https://play.google.com/store/apps/details?id=ultimate.apps.crackscreen2
public void onBackPressed() {
super.onBackPressed();
//Do ur operation here
}

android: dim the screen when application is in background

I am developing a small app which shows passwords of the user through a Dialog screen.
When home button is pressed, I need to dim the screen (on the multi tasking window) so that any other person cannot see the password.
When user re-opens the app, it asks an application lock. But if the user leaves the password Dialog open and presses the home button, dialog and the password which user last looked at stays visible (on the multi tasking window) for a while (3-4 seconds!!) until a new dialog asks the lock.
So far I tried ever possible dialog.dissmiss() options. Dialog dismisses only when app is opened again (until a new lock dialog appears) even I put dismiss() in onPause, onStop etc.
Any idea appreciated.
I also tried,
android.os.Process.killProcess(android.os.Process.myPid());
this.finish();
System.exit(0);
none of them actually worked.
Suggestion 1: Double-check your implementation. Tying your dialog to the activity lifecycle seems like a good idea (especially to avoid leaked window errors as described here)
The following example works out well for me (with coachMark being derived from Dialog)
#Override
protected void onResume()
{
log.debug("onResume");
super.onResume();
// Show the coachMark depending on saved preference values
coachMark.mayBeShow();
}
#Override
protected void onPause()
{
log.debug("onPause");
// Hide the coachMark if it is showing to avoid leakedWindow errors
coachMark.maybeHide();
super.onPause();
}
onPause definately gets called when you press the home button, so if this approach does not work for you, try not recreating the dialog in the restarting part of the acitivty lifecycle (onRestart(), onStart() and onResume()) and see, if it gets dismissed correctly.
Suggestion 2: Should all of the above fail, you might consider overriding the home button as described here. I highly advise against it though, since this may cause the app to work in an way that the user does not expect it to.

Android Home Button disable back

if a user tapped the home button and open the app after that, how to not allow back? eg don't allow users to go back to screens that they seen before tapping the home button. It should be treated as a new session
This sounds like a bad idea, as it blatantly goes against the Android task/navigation guidelines.
The user expects to be able to back out to the previous screen after resuming a task... and preventing it will potentially piss off a lot of users.
Please, please, please read these documents before you risk destroying the user experience.
App structure
Navigation
Tasks and back stack
The home button cannot be overridden nore should it, if you dont want the user to go back to the activity they left when the home button was clicked then on the on pause of the activity just pop the backstack to where you want to be.
see this answer
If you want to end your Activity once it is no longer visible then finish your activity in your Activities call to onStop().
#Override
protected void onStop() {
super.onStop();
this.finish();
}
This will finish your Activity with no chance of onRestart() being called. Be careful with this method because users expect to resume the app instead of having to start over, but there are cases where this would be the accepted protocol. See more on Navigation and the Activity LifeCycle.
Edit:
Also see the question Android-Quittings an application - is that frowned upon? specifically this answer.

Categories

Resources