android - why alertDialog showing on homescreen? - android

I have a alertDialog prompt during onCreate of Application Class
(i added <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />)
and I have set setCancelable to false and it did succeessfully prevent user from pressing back button.
This is what I have achieved so far and this is also what I want.
However, when I press home button, the app did "disappear" but somehow the dialog still showing on home screen, and this is what I DON'T WANT.
What I expecting is that the back button should do nothing(this is what I have achieved).
and when user click home button, the whole app Including the Dialog should disappear. But somehow when I click home button the Dialog still appearing on the home screen...

This is because when you are creating the AlertDialog, you probably pass an ApplicationContext instead of just Context.
By following the android framework guideline, you should not make any UI changes in your Application class. Do it in your Activity of Fragment.
So do this in your Activity class:
new AlertDialog.Builder(this)
or this in your Fragment:
new AlertDialog.Builder(getContext)
Then it will disappear if your application goes into the background state.

Try this,
myDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog1, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
return false;
}
});

This will do the trick:-
dialog.setOnKeyListener(new Dialog.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogs, int keyCode,
KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
return true;
}
if (keyCode == KeyEvent.KEYCODE_HOME) {
// Do your stuff here...
return true;
}
return false;
}
});

Related

How to make device back button cause onCreate?

When you press back in ActionBar, it causes onCreate on the previous activity. How to do that with device back button aswell? In preference activity, if a certain preference is changed, I want main activity to be re-created when user presses back on bottom of their device.
You can override onBackPressed() and start your Activity yourself.
You call onNavigateUp() at any time in your Activity to get the same behavior as clicking the up button.
For example:
#Override
public void onBackPressed() {
if (myPreferenceHasChanged) {
onNavigateUp();
} else {
// Use the default behavior
super.onBackPressed();
}
}
Did you try something like this?This should solve your question
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// your code
return true;
}
return super.onKeyDown(keyCode, event);
}

Back button stuck in endless loop?

My app is currently stuck in an endless loop with the physical back button. The program is set up to load a splash screen and then transition to a main menu. Once at the main menu the user can switch to another activity of their choice. For example: The new game activity. Once the user is at the new game activity I want them to be able to hit the back button and have it take them to the main menu. Once back at the main menu, if they hit the back button again I would like it to exit the game.
This is what I am using for each activity:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
}
return super.onKeyDown(keyCode, event);
}
It works properly and takes the user back to the main menu without a problem. But if the user hits the back button again once at the main menu it will take them to the screen they just escaped from. So it just loops back to the previous screen every time.
This is how the main menu is set up:
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
}
return super.onKeyDown(keyCode, event);
}
If I press the back button as soon as the main menu loads the first time it will work properly and close the game. It only messes up if I've hit the back button previously to get from one screen to the main menu.
Update:
Okay that kind of worked. When I hit the back button at the main screen the music stops and it acts like it's trying to close the app but it flashes back to the main screen again. I have to hit it twice every time.
You should call finish() inside the onKeyDown method of your activities so your current activity is actually removed from the stack.
Something like this should work:
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
startActivity(new Intent(NewGameActivity.this, MenuActivity.class));
finish();
}
return super.onKeyDown(keyCode, event);
}
First (from comments) add return true; within the if statements (to indicate that you have handled the event), like so:
// Main menu code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
stopService(new Intent(MenuActivity.this, BGMusicService.class));
MenuActivity.this.finish();
return true; // Add me!
}
return super.onKeyDown(keyCode, event);
}
Additionally, I believe because you're "starting" the menu activity again, you have to clear the back stack (the order of activities used so far) before opening it. You should be able to achieve this using Intent.FLAG_ACTIVITY_CLEAR_TOP (or maybe Intent.FLAG_ACTIVITY_CLEAR_TASK). Something like this:
// Other activity code
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
Intent menuIntent = new Intent(NewGameActivity.this, MenuActivity.class);
menuIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add me!
startActivity(menuIntent);
return true; // Add me, too!
}
return super.onKeyDown(keyCode, event);
}
You haven't called finish() in your other activities so when you call finish() on the menu it just closes itself and the other activity is exposed

Android : override BackButton when menu is visible

Let's say I have an activity A that overrides the back button to show some dialog and that this activity has a menu.
So, when the back button is pressed the dialog shows up, but if the user press the menu button and then the back button, the dialog isn't shown. How can I make the behavior for the back button be the same whether the menu is visible or not?
You need to override the BackButton.
onBackPressed()
{
closeOptionsMenu(); // to close the Options Menu if it is visible
//your code here
}
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
}
return true;
}
hope this help

I want Activity to finish if i press ESCAPE button on an AlertDialog in an Activity

I have 3 Activities A,B & C.
I present some video links in Activity A in a list. If user clicks on an item, i load Activity B in which i do some verification of user's data such as ask him to provide his Login info in an AlertDialog. I present two buttons in this dialog to Login & Cancel. If user presses Login i verify his info and open Activity C where i play his selected video. But If user presses Cancel, i finish() Activity B to load Activity A.
This works fine if user only interacts through burrons.
But Issue arises that when i press ESCAPE button on my keyboard/D-Pad when the AlertDialog is open. The dialog disappears but the Activity B does not finish(). I have overridden onKeyDown of Activity B where i do the following.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
|| keyCode == KeyEvent.KEYCODE_ESCAPE) {
Log.d(TAG, "onKeyDown : calling finish() manually");
finish();
return true;
} else
return false;
}
But it does not work WHEN i press ESCAPE on an open AlertDialog.
How can i achieve this?
Capture the setOnKeyListener event of AlertDialog and do paste the following code in it. It will also close the activity on BACK button:
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_ESCAPE)
{
yourActivity.finish();
return true;
}
return false;
}

How to disable the back button when the alert box is on the screen

I used onKeyDown function in the activity.....but when back button
button is clicked it first cancels the dialog box and goes to our
activity...I want both either both activity and dialog box closed when
clicking the back button or disable the back button when the dialog
box is shown...
can any one suggest any solutions for this....
Thanks in advance,
Update
Hello Thanks for your answer.
The progressDialog with .setCancelable(false); is working fine.
But here I want different thing.
When the progress dialog is running then i will press the BACK key and i want to show an alert dialog so that the user can notify that the progress is running.
Is there any solution about it?
Please help me.
Thanks in advance.
Did you try setting its setCancelable() property to false
Something like this
progressDialog.setCancelable(false);
May be this will help in your case:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// Show your Alert Box here
}
return false;
}
I just want to improve #MoJo answer
alertDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//Your handler
return false;
}
});
Personally a cleaner solution was to finish the LoginActivity before starting the intent instead of setting Flags in the bundle of the new activity or overriding any methods. Try something like this below where Login is your login activity and Home is the first activity after successfully login in the user.
finish();
Intent intent = new Intent(Login.this, Home.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
"I want both either both activity and dialog box closed"
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
finish();
return true;
}
});
"or disable the back button when the dialog box is shown" This can block going back from dialog. When creating it dynamically, add:
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
return true;
}
});
Returning true is preventing this dialog from closing, and it cannot be closed by clicking back. Also I suggest adding:
dialog.setCanceledOnTouchOutside(false);
What will prevent user from dismissing dialog by clicking outside of it.

Categories

Resources