I want to show alert when user presses home button on device(do you want to exit the app ?)
How to do it ?
You can't handle Home button click, because of Android policy (Home button click event handling android). Of course, you can use onPause()/onStop() method of your current Activity, but your application will be moved to background too quick and user will not see your dialog, I think.
Also, note that Home not closes the app - just moves to background. User usually close app by pressing Back on main activity, try to handle it:
#Override
public void onBackPressed() {
// your dialog here
}
Is is not possible for Android apps to override the functionality of the home button. The best you can do is show the dialog when the user presses back in your topmost Activity.
You can find more information at the following SO answers:
https://stackoverflow.com/a/7240268/3214339
Android Overriding home key
Write the show alert dialog code in onPause() it will work perfectly.
Related
Problem Description
I'm using Native Screen Recording functionality provided by Android. When I start recording System popups Dialog and ask user to confirm that screen can be recorder. If user press outside dialog dismissed (same behaviour when he/she press cancel) when press Start Now Recording Start.
Question
How I can detect what user pressed
Cancel
Start Now
Dismissed on press outside
How can I disable dismiss on press outside of dialog
Is there a way to avoid showing dialog?
Screenshot
How I can detect what user pressed
You can determine if the user pressed "Start Now" as part of your normal media projection flow. For example, when you pass the onActivityResult() data to getMediaProjection(), getMediaProjection() will only give you a MediaProjection object if the user pressed "Start Now".
You cannot distinguish between "Cancel", "click outside the dialog", pressing the BACK button, pressing Esc on a device with a physical keyboard, or any other means of dismissing this dialog.
How can I disable dismiss on press outside of dialog
The decision on how to handle this is up to the system, not you.
Is there a way to avoid showing dialog?
No.
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.
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?
I have a timer which runs continously. When I press the BACK button I made a dialog to appear where you can quit from that intent or go back and cointinue the timer what has been stopped by the BACK button. Well if I click on the contimnue, the onResume() method makes the timer continue and it works good. But, if I press the back button when the dialog is on the screen I want the timer to go on just like if I press the Continue on the dialog. But instead, I press the back button and nothing happens, the timer is stopped and it is not good for me since some of my methods only works if the timer is going or it is stopped by the dialog. But if there is no dialog and the timer is stopped numerous potential errors can happen. So how can I stop the user to press the back button when the dialog is on the screen?
I tried something like this:
if ((keycode==back) && a=0 ) {... a=1 , onPuase()} // dialog comes in onPause() just happened
else ((keycode==back) && a=1 ) {... a=0, onResume()} //I want onResume() to happen here
But it is not good. The dialog appears on the first Back button then it disappears on the second Back (nothing happens here). The timer is still stopped here however the third back button starts the timer. So there is an unecessary Back which can cause troubles since the useres wont know that they have to press it again...
A few advices:
Do not call onResume/onPause manually, only system should make it. Else you'll have unexplainable issues on various devices.
You really want to use OnDismissListener ( http://developer.android.com/reference/android/content/DialogInterface.OnDismissListener.html ). As starting from ICS, dialog can be dismissed not only by pressing Back key, but also by tapping somewhere on screen, outside the dialog.
If you want to prevent dismissing the dialog by "back" and "tapping out of dialog" - use setCancellable(false) http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean) for the dialog.
Good luck
If you want to be notified when user pressed BACK while your dialog was displayed, use OnDismissListener
implement OnDismissListener in your DialogClass
and override OnDismiss method
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
//you can control back button from here
}
I have an activity that starts on demand of the user.
The user can demand it from several activities.
The thing is I want to give the user an explanation before he has to handle that activity.
I thought about creating a Dialog, giving the user only an OK button to tap on.
But It will be ugly because:
It has to return a value (in my case there is no value I have to return)
The Dialog will have to start the new activity, then when the user presses 'back' button, it will return to the Dialog
Also, if I choose to return to the activity that showed the dialog and start the new activity from there, I'll have to do this in several places (like I explained in the second line of this questions)
Any ideas?
Thanks!
Not sure what you mean by 1. but if you call dismiss() in the onClickListener that handles your OK button, it will not be shown again after you come back from the started activity when you press the back button.
Do make it easier to reuse the dialog, you should create a custom class that handles the dialog. Then you can easily show the dialog from different activities.