Search code examples
androidandroid-activityime

Android: Method is called twice if imeOptions is used


Layout:

    ....
    <EditText
        ....
        android:hint="@string/email"
        android:imeOptions="actionSend"/>
    <Button
        ...
        android:onClick="sendMessage"      <<<- both must call it 
        android:text="@string/send" />

Then binding in code:

( (EditText) findViewById(R.id.email) ).setOnEditorActionListener(new OnEditorActionListener()  {
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                sendMessage(findViewById(android.R.id.content));
                return false;
            }
        });

Where sendMessage is

public void sendMessage(View view)
{
    ....
    intent.putExtra("email", getEditContent(R.id.email));   
    startActivityForResult(intent, 0);
}

When I press button everything is fine. When I press "Done" in imeOption (keyboard) two Activities starts at the same time.

What am I doing wrong?


Solution

  • Change the return value of the onEditorAction method from true to false.

    Actually, I think the method is called twice because of the KeyEvent. Try logging the type of the arg2 parameter to check it. If you confirm this, instead of returning false, you can add an if/else to check the correct event.