setCancelable(false) and onBackPressed conflict - android

I have a problem
I have a non cancelable custom dialog
which mean this custom dialog can only close if presses buton inside the custom dialog, so it won't cancel on backpress or click outside
I tried setCancelable(false) and it works however in my activity I have a onBackPressed and whenever my non cancelable dialog show onBackPressed wont trigger when I click back button because I think they conflict
is their a solution to do this?
EDIT: The purpose is I want the user to click button ok, or skip inside the custom dialog which means this dialog is required before proceeding to next activity
also in onBackPressed since I am using fragment whenever user press back it changes to previous fragment
sorry for lacking of explanation
my code is this
Dialog
dialog_welcome_navigation = DialogUtils.showCustomDialog(context, R.layout.dialog_welcome_navigation);
dialog_welcome_navigation.setCancelable(false); // disable closing dialog with back pressed
dialog_welcome_navigation.setCanceledOnTouchOutside(true);
and the onBackPressed
#Override
public void onBackPressed(){
Log.d("TAG", "--back--");
}

After searching I have found a solution thanks to this SO answer
https://stackoverflow.com/a/25251122/3481654
I add a setOnKeyListener on my dialog
dialog_welcome_navigation.setOnKeyListener(dialogWelcomeNavigationOnKey);
private DialogInterface.OnKeyListener dialogWelcomeNavigationOnKey = new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == android.view.KeyEvent.KEYCODE_BACK) {
dialog_welcome_navigation.dismiss();
// move other fragment
return true;
}
return false;
}
};

Related

OnEditorAction not being called inside a DialogFragment

I wanted to add a neat functionality to my Dialog. When the user presses DONE button on the keyboard after editing one of EditTexts inside the dialog, it would simulate onPositiveClick of the dialog so that the data inside the EditTexts gets handled properly and the dialog is dismissed. This is much better than first entering the data and then pressing the dialogs OK button. My dialog extends DialogFragment and implements OnEditorActionListener and this is my listener method:
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE)//check if done was pressed
dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
return false;
}
But the problem is that this method is never called when i press DONE on my keyboard. Do you know what causes that?

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

Android Back Button Utilization Inside ActivityGroup

I am developing an application using TabHost. I am using android default back button to move back to previous activity from current activity by overriding onBackPressed() method inside ActivityGroup of each tab.
Now , the problem is , in one of my activity i have an EditText which get focused when the activity starts. Then , if i press back , it does not go to previous activity , instead it closes the application. By searching about the problem on internet what i have found is that when the EditText get focused which is a child view of activity's view , the activity loss focus and then if back button is pressed , for lack of focus on the current activity it closes the application. Still i am little bit confused or can be say not clear about the problem.
So, any how , i have managed to set and remove focus over EditText on run time using code. But still now , as the EditText does not have focus , if the back button is pressed , it closes the application. I am really confused what's actually going on. So , if anyone have any idea or solution about the problem , please help on the issue. I will appreciate that very much. Thanks.
You can override this behavior by adding Key Listener to your EditText. Try this out,
name_edit.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN
&& event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
Log.i("Back event Trigered", "Back event");
activitygroup.back();
}
return false;
}
});
try this..
#Override
public void onBackPressed() {
onKeyDown(KeyEvent.KEYCODE_BACK, new KeyEvent(KeyEvent.ACTION_DOWN,KeyEvent.KEYCODE_BACK));
super.onBackPressed();
}
try this
public void onBackPressed() {
startActivity(new Intent(currentActivity.this, previousActivity.class));
finish();
}

Android: show white background when search button pressed

When I press the search key on my device, I want it to show a white background. However, when I press the back button, I want the previous activity's background to be restored. ivBackground is a variable I added to my relativelayout which I turn VISIBLE to show the white background.
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_SEARCH) {
ivBackground.setVisibility(View.VISIBLE);//WHITE IMAGEVIEW
return false;
} else if (keyCode == KeyEvent.KEYCODE_BACK) {
ivBackground.setVisibility(View.GONE);
return true;
}
return super.onKeyDown(keyCode, event);
}
While the above code works, the problem is that when I press the back button, the white screen still remains. It only goes away if I press the back button once again. Any solutions?
On the relevant activity, you can get a reference to a SearchManager object. On this, you can set an OnDismissListener, which is called the when search UI is dismissed e.g.
this.searchMgr = (SearchManager)this.getSystemService(Context.SEARCH_SERVICE);
this.searchMgr.setOnDismissListener(new OnDismissListener() {
public void onDismiss() {
ivBackground.setVisibility(View.GONE);
}
});
To make the white background visible, you can override onSearchRequested inside your activity class, which is called when a user signals the desire to start a search
#Override
public boolean onSearchRequested() {
ivBackground.setVisibility(View.VISIBLE);
return super.onSearchRequested();
}
Hope this helps!
How about setting a SearchView.OnCloseListener or a SearchManager.OnDismissListener that also hides your view? This seems like the right solution anyway since the two events are logically connected. If you later dismiss the search view programmatically for example, you don't have to worry about separately dismissing the background view.

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