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.
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.
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);
}
});
...