Sunday, March 11, 2012

TextWatcher and change of device orientation

I encountered an interesting problem with a TextWatcher listening for changes in an EditText. The afterTextChanged method was called, each time, the device orientation changed. An answer on Stackoverflow let me understand what was happening: Android recreates the activity, and the automatic restoration of the state of the input fields, is happening after onCreate had finished, where the TextWatcher was added as a TextChangedListener. The solution to the problem consisted in adding the TextWatcher in onPostCreate, which is called after restoration has taken place. You can find the source in this commit of project MyExpenses on Github.

7 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Please update the Github link. Its dead. Please reply to this thread if done. thank you.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. The solution described in this blog post is no longer used in the referenced Github project, you fill find it on commit https://github.com/mtotschnig/MyExpenses/commit/bfad3088227583cf39c4def2264693fbc36457a9

    ReplyDelete