Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android keyboard still visible after launching Email intent

EDIT: Solved. Answer posted separately below

I'm launching the built-in Intent.ACTION_SEND "chooser" so the user can select how to send a message from my application. It works OK, but if I hit 'Discard' in the launched Email program, it returns to my application with the on-screen keyboard still visible. I've tried closing it with various incantations of imm.hideSoftInputFromWindow (...) but to no avail. Any ideas how to fix this?

This is how I'm launching the 'chooser' and attempting to close the keyboard in onActivityResult(). Note that tabHost is a static member in my main application (MainApp) which holds the tabHost object used to create the 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();
    }
  }

 ...

}
like image 588
wufoo Avatar asked Jul 17 '12 19:07

wufoo


1 Answers

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);
}
like image 80
Laith Alnagem Avatar answered Sep 27 '22 18:09

Laith Alnagem