Recently while developing an app, I faced an issue. I have searched a lot on google but couldn't find any solution. In the end I came across this Android issue tracker
To explain my issue, I have made a sample App.
Basic Working of my Sample App
- I have a screen, which has an EditText, a Button and a RelativeLayout.
- Width and Height of RelativeLayout is 0px. It is just a view to move focus away from EditText.
- When App is launched focus is on RelativeLayout, not on EditText(so that there is not blinking cursor in it.)
- When a user clicks on Button I just move focus to RelativeLayout using requestFocus() call on RelativeLayout.
- When user taps on EditText, keyboard comes up. I can enter text in that.
What I want to achieve
- If I change orientation of phone when keyboard is visible then after orienation changes, keyboard should stay.
- If keyboard is visible and some other activity comes on top of it for e.g. alarm, facebook chat heads, opening something from notification area, locking unlocking device, etc.. then on returning back to sample app keyboard should be visible.
How I am achieving this
- In onSaveInstanceState(), I check if focus is on EditText then put a boolean variable in Bundle.
- In onStop(), I am setting a one boolean flag
wasEditing = true
. - In onRestoreInstanceState(), I checked if Bundle has flag value set in onSaveInstanceState(). If yes then I am make
wasEditing = true
. - In onResume(), I check this
wasEditing
and if it is true, I request focus for EditText. - After that I call
imm.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT,resultRec)
Where I am getting problem
Sometimes after executing this call, Keyboard is not visible in few cases, like during orientation change.
When I put logs I have found this function is returning false
But if I make this showSoftInput()
call with some delay of 100ms using mEditText.postDelayed()
in onResume()
everything works fine.
Question In what cases this function returns false and why delay is working?
Note Although I have solved my problem using delay, but I still want to know why it is behaving like that.