Unable to get the text from SearchView.SearchAutoComplete onCreateView in Fragment

时光怂恿深爱的人放手 提交于 2019-12-11 16:35:59

问题


In my FirstFragment's onCreateView() method I have the following elements:

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    initSearchView();
    String selectedItem = search.getText().toString();
    Log.d(TAG, "onCreateView " + selectedItem); //Always empty
}

As you can see, I call initSearchView() method which looks like this:

private void initSearchView() {
    SearchView searchView = rootView.findViewById(R.id.search);
    search = searchView.findViewById(androidx.appcompat.R.id.search_src_text);
    search.setOnItemClickListener((adapterView, view, position, id) -> {
        Item Item = (Item) adapterView.getItemAtPosition(position);
        search.setText(item.getItemName()); //Set item name
        goToItemFragment(item.getItemName());
    });
}

As you can see, once an item is selected, I set the name of that item to the search object. This works fine since I see that the name of the item is placed inside the search.

Here is the method that I use to go to the SecondFragment:

private void goToISecondFragment(String itemName) {
    ActionFirstFragmentToSecondFragment action = actionFirstFragmentToSecondFragment();
    action.setItemName(itemName);
    Navigation.findNavController(rootView).navigate(action);
}

The problem arrives when I get back from SecondFragment to FirstFragment. Even if I see the selected item name in the search, when the fragment starts, that log statement is always empty, meaning the it doesn't see that text in the search.

How can I get that visible text from search when I get back from SecondFragment to FirstFragment? Thanks

来源:https://stackoverflow.com/questions/58730536/unable-to-get-the-text-from-searchview-searchautocomplete-oncreateview-in-fragme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!