OnQueryTextChange is triggered after application resumes

I crashed with an animated search onQueryTextListener. When the activity and fragment are created first, it works well. Then I press the home button, open other apps, do some work to erase the search activity data, and then return to the app. And when the activity and fragment are resumed by the QueryTextChange method, it is started by itself. I tried this issue Fragment Replacement Triggers onQueryTextChange on the search page but it didn't help, only helps when searching for SHOW_AS_ACTION_NEVER, but I can't see the searchview in this case. How can I prevent the OnQueryTextListener from starting up itself? Fragment fragment

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    searchView = new SearchView(getSherlockActivity().getSupportActionBar()
            .getThemedContext());
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (newText.length() > 0) {

                fpAdapter.getFilter().filter(newText);
            } else {

                loadData();

            }
            return false;
        }
    });

    TextView searchText = (TextView) searchView
            .findViewById(R.id.abs__search_src_text);
    searchText.setTextColor(Color.WHITE);

    searchText.setCursorVisible(false);

    ImageView searchButton = (ImageView) searchView
            .findViewById(R.id.abs__search_button);
    searchButton.setImageResource(R.drawable.search_menu_button);
    LinearLayout searchEditFrame = (LinearLayout) searchView
            .findViewById(R.id.abs__search_edit_frame);
    searchEditFrame.setBackgroundResource(android.R.color.transparent);

    View searchPlate = searchView.findViewById(R.id.abs__search_plate);
    searchPlate.setBackgroundColor(Color.argb(0, 0, 0, 0));
    menu.add("Search")

    .setActionView(searchView)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    final MenuItem searchMenuItem = menu.getItem(0);
    final Animation in = AnimationUtils.loadAnimation(getSherlockActivity()
            .getApplicationContext(), R.anim.search_in);
    final Animation out = AnimationUtils.loadAnimation(
            getSherlockActivity().getApplicationContext(),
            R.anim.search_out);
    searchView.setQueryHint(getResources().getText(
            R.string.search_messages_hint));
    searchView.setIconifiedByDefault(true);
    searchView
            .setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view,
                        boolean queryTextFocused) {

                    if (!queryTextFocused) {

                        // searchView.startAnimation(out);
                        searchMenuItem.collapseActionView();
                    } else {
                        searchView.startAnimation(in);

                    }
                }
            });

    super.onCreateOptionsMenu(menu, inflater);
}

      

Update: This only appears on HTC sensation XL with Android 4.0.3, on 4.2. I don't see this problem.

+3


source to share


2 answers


Found the only solution - set listener in onResume:



@Override
public void onResume() {

    super.onResume();

    searchView.setOnQueryTextListener(new OnQueryTextListener() {

    @Override
    public boolean onQueryTextSubmit(String query) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        if (newText.length() > 0) {

            fpAdapter.getFilter().filter(newText);
        } else {

            loadData();

        }
        return false;
    }
}); }

      

+3


source


use this code



        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(final String s) {

            if(searchView.getWidth()>0)
            {
                // your code here
            }

            return false;
        }
    });

      

+2


source







All Articles