11

I tried to implement search using EditText. whenever a text is typed in the EditText request is sent with the typed text in onTextChanged() method. When I change the orientation of the phone with the results displyed, onTextChanged() method is called again with same text. How can I avoid redundant call to onTextChanged() method on orientation change.

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    final String enteredKeyword = s.toString();


    if(isFragmentVisible && !enteredKeyword.equals("")) {

    searchTimer.cancel();
    searchTimer = new Timer();
    TimerTask searchTask = new TimerTask() {
    @Override
    public void run() {
          searchUser(enteredKeyword);
    }
};
searchTimer.schedule(searchTask, 1500);
Log.i("", enteredKeyword);
}
}
1
  • 1)You may be set the Previously entered text to the same EditText. 2)To avoid this you can check the string which is already performed or not in onTextChanged method before calling your function. Commented Jan 6, 2015 at 6:33

2 Answers 2

15

I've got this problem just. So I moved addTextChangedListener to the post method of EditText in the onCreateView:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ...

    EditText mSearchQuery = findViewById(R.id.search_query);
    mSearchQuery.post(new Runnable() {
            @Override
            public void run() {
                mSearchQuery.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        //Some stuff
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
            }
        });
}
Sign up to request clarification or add additional context in comments.

3 Comments

hey can you please elaborate on why this worked? Why did you have to add a textchangelistener to the ui thread?
This delays adding the listener until the next screen render, which happens after EditText's savedInstanceState restores the text it had before. My guess is this could also work if you added this in a different lifecycle hook, like onResume, for example.
This hides a valid inline error when device screen is rotated
-2

You need to override onConfigurationChanged method to get callback whenever orientation is getting changed.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // portrait
    }
}

Add below line in manifest

android:configChanges= "orientation"

Now based on the callback you can do whatever you wanted to do.

2 Comments

thanks daud. but i dont have to do anything on configuration change. I need to stop the call to onTextChanged() method when orientation is changed.
see .. if activity is recreated on configuration change, onTextChanged will be called again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.