onQueryTextChange triggered after app resume
Asked Answered
D

4

7

I am stumbed with animated searchview onQueryTextListener. When activity and fragment created first it works nice. Then I press home button, open other apps, do some work there to wipe the data of searchview activity and then return to the app. And when activity and fragment resume onQueryTextChange method is triggered by it's own. I tried this issue Fragment replacement triggers onQueryTextChange on searchview but it did not help, helps only when searchview SHOW_AS_ACTION_NEVER, but in this case I can not see searchview. How to prevent self-triggering of OnQueryTextListener? Snippet from 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 appears only in HTC sensation XL with Android 4.0.3, on 4.2 I don't see this problem.

Deforest answered 12/8, 2014 at 19:27 Comment(0)
D
6

Found the only one 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;
    }
}); }
Deforest answered 13/8, 2014 at 21:58 Comment(0)
C
4

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;
        }
    });
Campobello answered 10/4, 2015 at 5:58 Comment(0)
D
3

You can set to use the SearchView only when it's in focus. I had a similar problem where the search was performing in my fragment every time when users resumed it. I solve it with the following method:

-Add a boolean to see when SearchView is in focus:

//by default the SearchView isn't in focus so set it to false. private boolean shouldSearch = false;

searchView.setOnQueryTextFocusChangeListener((view, hasFocus) -> {
        if (hasFocus) {
            shouldSearch = true;
        } else {
            shouldSearch = false;
        }
    });

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

        @Override
        public boolean onQueryTextChange(String newText) {
            if (shouldSearch) {
               //do your search here
            }

            return true;
        }
    });
Dionnedionysia answered 27/2, 2020 at 17:46 Comment(0)
O
1

This is a known issue. You can fix by checking to see if the search is "iconified" i.e only showing the magnifying glass icon or if it is expanded ( which means the user is interacting with the search view)

        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                //your code here
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //this will catch firing event
                if (searchView.isIconified) {
                    return true
                }
                //your code here
                return true
            }
        })
Orator answered 5/2, 2021 at 16:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.