I have an app with two activities, and sometimes, I need to switch activity and at the same time open up the search input in the actionbar of the activity that's just been resumed. Everything works fine, except that I can't get the keyboard to come up. The relevant bits of my code are below (NB: the boolean startsearch is set true
as a result of switching activities if the search input is required):
public class MyActivity extends Activity {
private InputMethodManager imm;
public boolean startsearch;
private MenuItem DestinationTxt;
private SearchView mySearchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// various initialisation, and then:
startsearch = false;
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.action_menu, menu);
DestinationTxt = menu.findItem(R.id.actionbar_search);
mySearchView = (SearchView)DestinationTxt.getActionView();
// more menu create stuff appears here
}
@Override
public void onResume() {
super.onResume();
if (startsearch) {
DestinationTxt.expandActionView();
imm.showSoftInput(mySearchView, 0);
}
}
}
and the relevant bit of the action_menu.xml is
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/actionbar_search"
android:orderInCategory="1"
android:showAsAction="always|withText|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:icon="@drawable/earth_2508858_search_en"
android:inputType="textPostalAddress"
android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"></item>
</menu>
As I said, this mostly works, because the action bar search does get the focus when the activity resumes. But the keyboard doesn't appear, even though (as you can see from the code), I've explicitly requested it. Can anyone tell me what I'm doing wrong, and what I need to do to get the keyboard to come up?