title: EditText.setText() gives index out of bounds
body: <p>I'm having an error I can't seem to wrap my head around.  I'm writing an app that allows the user to send text from an <code>EditText</code> to a <code>TextView</code>.  If the user makes a mistake, the user can hit the space key to bring the most recent text sent from the <code>EditText</code> to the <code>TextView</code>.  This works sometimes, but other times, it gives me an <code>IndexOutOfBounds</code> exception.</p>
  
  <p>textInput is an EditText, back1,2,3 are the three most recent strings (with back1 the most recent)</p>
  
  <pre><code>public void onTextChanged(CharSequence s, int start, int before, int count) {
  
      if(count==1&amp;&amp;before==0&amp;&amp;s.toString().equals(" ")){
          textInput.setText(back1);
      }else if(s.toString().equals(back1 + " ")){
          textInput.setText(back2);
      }else if(s.toString().equals(back2 + " ")){
          textInput.setText(back3); //causes error if back2 &gt; back3
      }
  
      textInput.setSelection(textInput.getText().toString().length());
  </code></pre>
  
  <p>The above code checks if the user hit the space key, and if so, what to do with it.  If the user pressed space on an empty EditText, they get the last thing they sent.  If they hit space again, they get the next to last thing they sent, and so on.  This is still a bit rough, but I hope you get the idea.</p>
  
  <p>The OutOfBounds exception comes from taking a large item in the <code>EditText</code>, hitting space, and setting the <code>EditText</code> to a smaller string.  I assumed it was because the cursor is at the end of the <code>EditText</code> and could no longer be there when the text got smaller, so I tried adding <code>textInput.setSelection(0)</code> right before the <code>setText()</code>.  That didn't help.  I also tried setting the <code>EditText</code> to <code>setText("")</code>.  That didn't work either.  If I comment out the lines of <code>setText(back#)</code>, everything works fine.</p>
  
  <p>An example:</p>
  
  <p>A user types in "hello", "hi" and "hey" in that order.</p>
  
  <p>back3 = hello, back2 = hi, and back1 = hey.</p>
  
  <p>Hitting space once will set the EditText to "hey"</p>
  
  <p>A second tap will crash, since the <code>setSpan(3...4) ends beyond length 2</code>, presumably because back 1 is larger than back2.  It is supposed to set the text in the <code>EditText</code> to "hi"</p>