1

I have the following code I would like to execute in my onActivityResult() method:

InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodManager.showSoftInput(viewHolder.CertainEditTextView,InputMethodManager.SHOW_IMPLICIT);

This snippet should bring up the keyboard, allowing the user to type in to CertainEditTextView.

The issue: It does not work, and my theory is that this is because it runs before the Activity is fully loaded. I think this because if I run this code in a separate thread, after a delay of 1000ms, it works perfectly. This gives time the activity to load. This is not a real solution, and I am looking for a way to run this as soon as the Activity is done loading. onResume() would be a perfect method, but I only want to run this code snippet after onActivityResult is called.

All help is greatly appreciated.

Dick Lucas
  • 12,289
  • 14
  • 49
  • 76

2 Answers2

4

onResume is the perfect method I'd say since it is called after the onActivityResult call, you just need to set a flag such as a boolean to know if you're coming from the onActivityResult or another part of the lifecycle.

Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it. The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.

You will receive this call immediately before onResume() when your activity is re-starting.

Source

UPDATE

After the asker noted that this solution did not work for his specific need, You can consider using a ViewTreeObserver on the input to be notified when it's drawn and it should work. Something like this:

...
View yourView = (LinearLayout)findViewById(R.id.YOUD VIEW ID);
ViewTreeObserver vto = yourView.getViewTreeObserver(); 
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
    @Override 
    public void onGlobalLayout() { 
        yourView.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
        //We are now sure the view is drawn and should be able to do what you wanted:
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        InputMethodManager.showSoftInput(viewHolder.CertainEditTextView,InputMethodManager.SHOW_IMPLICIT);

    } 
});
...
Community
  • 1
  • 1
Juan Cortés
  • 20,634
  • 8
  • 68
  • 91
  • Juan, I agree with both you and Yazan that this would work. But I was thinking there would be a method that would allow me to prevent the use of state variables. Something that would allow me to add a runnable to the UI thread, and queue it at the end. Really appreciate the help. – Dick Lucas Jun 11 '14 at 14:42
  • UPDATE: Juan, actually, this solution does not work and I don't know why. I have to do a postDelayed of 50ms to get the keyboard to come up. This guy is having my exact same issue, and could only solve it using a post delayed: http://stackoverflow.com/questions/23380123/why-android-inputmethodmanager-showsoftinput-returns-false – Dick Lucas Jun 11 '14 at 16:15
  • 1
    Postdelayed is only a cheap solution, try using a view tree observer on the field, and execute the code then http://stackoverflow.com/a/7735122/363262 – Juan Cortés Jun 11 '14 at 17:13
0

do something like this(Pseudo)

private boolean SHOW_KEYBOARD = false;

onCreate(...){

}
:
:

:
:

.... onActivityResult ()
    if(result == RESULT_OK && eqCode == myReqCode){
        SHOW_KEYBOARD = true;
    }
}

onResume(){
    if(SHOW_KEYBOARD ){
        InputMethodManager inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        InputMethodManager.showSoftInput(viewHolder.CertainEditTextView,InputMethodManager.SHOW_IMPLICIT);
    }
}
Yazan
  • 6,074
  • 1
  • 19
  • 33