I navigate from Activity1 to Activity2
On Activity 2 I have a keyboard and this keyboard stays on the screen after selecting the back button and going to Activity 1.
This is how I fixed this issue
// This code is in Activity 2
#Override
public void onBackPressed() {
startActivity(intentForActivity1);
finish();
}
Is this a wrong solution to my problem?
Is it a bad idea to handle the back button manually?
Since you're capturing the back button press, most probably the soft keyboard does not receive the press and thus it does not hide.
Try hiding it yourself:
#Override
public void onBackPressed() {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
startActivity(intentForActivity1);
finish();
}
See Reto Meier's answer for more details on this method to hide the keyboard: Close/hide the Android Soft Keyboard
There's nothing inherently wrong with overriding the back button. Just make sure the behavior isn't confusing to the user.
Also, if you ever just want to hide the soft keyboard (say, you're switching between tabs or something like that), you can use InputMethodManager. Here's a thread where people discussed ways to do this.
Related
I have an logic like this: when enter the edit activity, i will show popup first, then show soft keyboard in the onDismissListener callback, but when i call showKeyboard directly in the callback, the soft keyboard doesn't show. Only i call the view.postRunnable, it will show as expected. As well the activity softkeyboard option is set android:windowSoftInputMode="stateHidden|adjustResize"
private void showKeyboard() {
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
}
Unless someone with deep knowledge of the inner gears of the UI framework come by to answer, everyone's answer will be a "best guess". So below is my best guess:
That is most likely related to the Window and WindowManager and how they interact with Views and keyboards.
The EditText being passed to the method have a token to your activity window, and here is my guess:
If the window is not in foreground, it can't show a keyboard. So when you post the method call, then that method gets executed after the Window from the Popup is gone, and the Window from the activity is in Foreground.
The onDismiss() callback is most likely called from non-UI thread, so you can't update your UI directly. That's why you need to post it to the UI thread with view.postRunnable().
I know that to hide soft keyboard I need to use code like this:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
My problem is that I starting the ACTION_SEND intent, and use Twitter app to handle it. I pass a message to tweet it. But if the user does not confirm the message, but clicks ActionBar back button, the Twitter activity is finished, and my app lication comes back to the front. But the soft Keyboard, called by Twitter does not hide. I have no idea how to get Twitter's WindowToken. Could anybody help me?
Another way is to do the same in AndroidManifest.xml file. You can annotate your activity with the following line:
android:windowSoftInputMode="stateAlwaysHidden"
which means your activity will always hide keyboard when receives focus.
I have found an unswer.
I had to add this code:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in the onResume() method
Just wondering if it's possible to prevent the keyboard from closing when the back button is pressed.
AKA, jump to the previous activity on one tap of the back button.
You can override onBackPressed() so that if the keyboard is showing you just call finish() on your Activity:
#Override
public void onBackPressed()
{
boolean keyboardIsShowing = // determine if keyboard is showing somehow.
if (keyboardIsShowing )
{
finish();
}
else
{
super.onBackPressed();
}
}
I am not sure an exact way to know if a keyboard is showing, but this link can point you in the right way:
How to check visibility of software keyboard in Android?
On a side note, users probably don't expect the Activity to close when the back button is pressed, they probably expect the keyboard to close. I would carefully consider your use case before implementing something like this.
With a bluetooth keyboard connected, the Note 2 forces the switch back to the Samsung keyboard. This is a problem, as the Samsung keyboard always pops up when I'm trying to type with my external, taking up screen space and defeating half the purpose. I could write an app that forces the switch back, probably, but I'd still have to hit Enter/OK everytime the keyboard connects. I disabled the Samsung keyboard altogether, but the OK button still shows up. Is there some sort of service that does this or something? Because if I can't block/disable whatever it is, it's going to mess with my workflow, seeing as I bought this K810 to switch between multiple devices.
Here's a code snippet I use in order to prevent the keyboard to popup at application start:
public void hideSoftKeyboard(View v) {
Activity activity = (Activity) v.getContext();
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
I think that if you can detenct the event with:
http://developer.android.com/reference/android/content/res/Configuration.html#keyboard
There are some flags, which indicate what's the inserted keyboard.
Or just try this one, even though i didn't test it:
public boolean onEvaluateInputViewShown() {
return false;
}
Let me know then of the results.
Thanks
How can I start a progress dialog when the default Home Key button of Android is pressed? If it's possible, then how?
I've tried using the onKeyDown method, but it doesn't work.
Not really, no. You cant override the home button as you would the back or search.Take a look at this discussion:
Overriding the Home button - how do I get rid of the choice?
Duplicates this: Android - capture/suppress Home and EndCall buttons events?
The short answer: you can not handle Home button, but you can write a replacement Home Screen application.
Because the 'Home' button is such an essential feature to android (It provides the user with a quick one-touch button to get back to their home screen) that they made it so we can't run custom code to modify what it does. We do however, somewhat have control over what happens when the user presses the menu, back, or search buttons.
It's always best to conform to Android's UI design guidelines, this article on application design is a great read on how exactly we should go about things.
The prevailing wisdom says it cannot be done.
Checkout this conversation:
Home key press Event Listener
Maybe the below code would work the way you want it to. But I don't think you can trap the Home key absolutely.
Override below method in your activity.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
}
Then after you can listen home key in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_HOME)
{}
});
Hope this may helpful to you.