I have an activity A (navigation drawer) that launches some fragments
The activity A contains this:
#Override
public void onBackPressed() {
//create dialog to close the app
}
The problem is that it doesn't matter the fragment where I am, when I touch the back button and I have an EditText focused in the current layout, it will launch the dialog.
This means that if I want to close the app, I have to be in the main window, and touch back button and it asks me if I want to leave.
But if I am in any fragment and touch the back button wile an EditText is focused, it also launches the dialog, overriding the behavior of the back button that I have set in that current fragment.
When I touch back button on a fragment this is what it excecutes if the EditText is not focused, which work perfectly:
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//stuff
}
}
});
If an EditText is focused, it will execute the code for the back button in the Activity.
Anyone?
I am sure that it is related to context, focus or something like that, but still can't figure out why
Related
I have an activity with search bar. On start, keyboard shows up.
If I hit back button, it dismisses the keyboard. I need to hit back button 3 times to dismiss activity. The first hit dismisses keyboard, second hit loses focus from search bar.
How to dismiss activity using back button?
You can catch back event and add your own destination activity as follows
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// finish current activity ( finish(); )
// add your new intent here
// Example:
// startActivity(new Intent(<your_context>, <destination_activity>));
}
return super.onKeyDown(keyCode, event);
}
I have a non-application window (i.e. a window with no activity context, that appears over other apps). This window is technically a View which has been added using WindowManager.addView() and TYPE_SYSTEM_ERROR.
I would like to remove this window when the Home button is pressed but it seems impossible to intercept the Home button press.
Is there any way to do this?
EDIT: My app does not have an Activity so cannot use any methods on the Activity class.
You can detect when a user press Home button with:
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
but you can't override system behaviour for that action.
Below method used for enabling Home button key press event
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
And,
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{
//The Code Want to Perform.
//Leave Blank no code like back press method so by pressng home button it will not go to home
//This can be used for long press home button also
}
return true;
}
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?
In default, when the drawer is showing and you press the BackButton it automatically close.
but I want when it's showing and user clicks on BackButton, close the app.
I tried these solution but didn't work :
Navigation drawer, handling the back button to go to previous fragments
Backbutton press from android navigation drawer
onBackPressed will not be called on drawer open or lets say if your keyboard is open then also your back key changes to down arrow. So when you press that down key your keyboard hides and your onBackPressed will not be called still.
Your app will be closed when you press the back button again. Same is the case with keyboard.
So first close the drawer, then press back button again, and then your app will finish
First override back pressed of your activity and then can check if drawer is open
mDrawerLayout.isDrawerOpen(GravityCompat.START);
If this return true simply close your app.
Important : Check your drawer gravity.
Try registering you fragment to back pressed like this:
public void registerBackButton()
{
if (getView() != null)
{
getView().setFocusableInTouchMode(true);
getView().requestFocus();
final BaseFragment frag = this;
getView().setOnKeyListener(new OnKeyListener()
{
#Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
if (event.getAction() != KeyEvent.ACTION_DOWN)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (isVisible())
{
return frag.onFragmentBackPressed();
}
}
}
return false;
}
});
}
}
Now in onFragmentBackPressed check if drawer is opened or not as mentioned above.
I use android:windowSoftInputMode="stateVisible|adjustPan" in my manifest file to open the softkeyboard when the main activity is launched.
This works great, apart from when i come back to the main activity from another using the back button; The softkeyboard does not reappear.
How do i make the softkeyboard appear when coming back to the main activity?
Thanks for any help in advance.
On back button it just remove the current activity from the stack and show the previous activity that is why softkeyboard is not getting opened . you can override the onKeyDown() method and on back button you can call your activity again.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// start your activity again here
Log.d(this.getClass().getName(), "back button pressed");
}
return super.onKeyDown(keyCode, event);
}