wufoo
wufoo

Reputation: 14571

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

 ...

}

Upvotes: 5

Views: 1731

Answers (3)

Laith Alnagem
Laith Alnagem

Reputation: 654

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

Upvotes: 5

wufoo
wufoo

Reputation: 14571

I ended up using the Context passed to getView() in my ArrayAdapter class which is instantiated in the L_Secondary class. It's not the best place to do this because every time the list is scrolled, or touched, or moved it's going to check for the keyboard being visible and close it if so. Nonetheless, it's a start. From here I can try and find a more efficient place to put 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);
     ...
}

Upvotes: 0

Daniel Jamison
Daniel Jamison

Reputation: 317

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

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

Upvotes: 0

Related Questions