Android keyboard is still visible after launching email intent

EDIT : Resolved. Answer sent separately below

I run the built-in selection tool Intent.ACTION_SEND, so the user can choose how to send a message from my application. It works fine, but if I delete Cancel in the running email program, it will return to my application and the on-screen keyboard will still be visible. I tried to close it with various imm.hideSoftInputFromWindow (...) spells, but to no avail. Any ideas how to fix this?

This is how I run "chooser" and try to close the keyboard in onActivityResult (). Note that tabHost is a static member of my main application (MainApp), which contains the tabHost object used to create tabSpecs.

public class L_Secondary extends ListActivity implements myConst
{   
    @Override
   protected void onCreate (Bundle savedInstanceState)
   {
     super.onCreate (savedInstanceState);
     setContentView(R.layout.l_people_secondary);

     // instantiate the custom array adapter class and pass it some info to build a ListView with. 
     ListView lv = getListView ();
     lv.setOnItemClickListener (oicl);
     A_secondary da = new A_secondary (this, android.R.layout.simple_list_item_single_choice, mPiecesArray, mPartsArray);

     setListAdapter (da);
   }

   ...  


   // after launching the email client, the keyboard stays visible 
   // over the Listview. Currently the keyboard gets forced to close 
   // in getView() of the ArrayAdapter class da, in onCreate() above                
   public void launchEmail () 
   {
    try
    {
     // use the builtin chooser for users mail app
     Intent sendIntent = new Intent(Intent.ACTION_SEND, Uri.fromParts ("mailto", "root@localhost", null));
     sendIntent.setType("text/plain");    

     sendIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "msg_subject");
     sendIntent.putExtra(android.content.Intent.EXTRA_TEXT, "msg_body");

     startActivityForResult (Intent.createChooser(sendIntent, "Send via which Application?"), 0);
    }
    catch (Exception e) 
    {
     Toast.makeText (this, "No activity was found to handle this action",Toast.LENGTH_SHORT).show();
    }
  }

 ...

}
+5
source share
4 answers

I ended up using Context passed to getView () in my ArrayAdapter class that was created in the L_Secondary class. This is not the best place to do this, because every time the list scrolls or touches, or moves, it will check the visibility of the keyboard and close it, if so. However, this is the beginning. From here I can try to find a more efficient place to place it.

@Override
 public View getView (int position, View convertView, ViewGroup parent)
 {
     View row = convertView;
     Context ctx = parent.getContext ();

     if (row == null)
     {
         LayoutInflater inflater = ((Activity) ctx).getLayoutInflater ();
         row = inflater.inflate (R.layout.li_secondary, parent, false);
     }

     // hide the keyboard when coming back from Email client Intent
     InputMethodManager imm = (InputMethodManager) ctx.getSystemService(Context.INPUT_METHOD_SERVICE);
     if (imm.isActive () == true)
         imm.hideSoftInputFromWindow (MainApp.tabHost.getCurrentTabView ().getApplicationWindowToken (),imm.HIDE_NOT_ALWAYS);
     ...
}
0
source

I found this worked for me by adding it to my onResume ()

protected void onResume()
{
  Handler h = new Handler();
  h.postDelayed(new Runnable() {
     @Override
     public void run() {
        InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        keyboard.hideSoftInputFromWindow(findViewById(android.R.id.content).getWindowToken(), 0);
     }
  }, 500);
}
+3

MAIL //ed - EditText

InputMethodManager imm = (InputMethodManager)this.getSystemService(Service.INPUT_METHOD_SERVICE);

imm.hideSoftInputFromWindow(ed.getWindowToken(), 0); 

show keyboard

imm.showSoftInput(ed, 0);

onRestart()

OR

you can also try this

<activity android:name=".YourActivity"
          android:windowSoftInputMode="stateHidden"></activity>

Thank.

0
source

I believe you can call the hideSoftInputFromWindow method in onResume ()

protected void onResume()
{
    InputMethodManager keyboard = (InputMethodManager)
    getSystemService(Context.INPUT_METHOD_SERVICE);
    keyboard.hideSoftInputFromWindow(userInput.getWindowToken(), 0);
}
0
source

All Articles